Rince Rince - 10 days ago 6
Java Question

Spring data rest sorting fields with underscores

We are using a very simple setup of

@RepositoryRestResource
on top of a
PagingAndSortingRepository
connected to a postgres database. Also we have configured
spring.jackson.property-naming-strategy=SNAKE_CASE
to return pretty json. It was all fine and dandy until we started sorting. As we have discovered - sorting requires us to provide the actual class field names (which we of course have in camel case):

get("/thing?sort=dateCreated,desc")


And when we try to do javascript friendly

get("/thing?sort=date_created,desc")


it fails miserably because jpa tries to split the parameter by the underscore.

Is there a simple way to have the path params the same format as we have them in the json that we are returning?

Answer

There is a bug for this - DATAREST-883. It was fixed and released. But then, due to regressions (DATAREST-909) this has been dropped in the very next release. I asked them on Github if they plan to have this again as this has bitten me in the past as well. We'll see what they have to say about this.

For now you can:

  • leave it be
  • go with the camel case property names
  • work around this (e.g. go with Alan Haye's answer) - this seems fragile IMHO, but probably will do in a short term.

The status of the feature in the recent spring-boot versions I've tested with:

  • 1.4.0 (spring-data-rest 2.5.2): not yet implemented -> code
  • 1.4.1 (spring-data-rest 2.5.3): works
  • 1.4.2 (spring-data-rest 2.5.5): dropped