5

Is there a way to map properties of Sort objects? Just a little example:

a Dto

public class OrderDto {
  private long totalAmount;
  private long openAmount;
}

and an entity

@Entity
public class Order {
  private long amount;
}

and a controller

@RestController
public class OrderController {


  @GetMapping("/orders")
  public Page<OrderDto> findOrders(Pageable pageable) {
   // Do something 
  }
}

I would now call /orders?sort=totalAmount,desc

Is it possible to map the sort property, in this case the totalAmount from the Dto to only amount to use this for a generated JPA query in a charmant way?

kaputnix
  • 123
  • 10

1 Answers1

2

I don't know exactly if I get your question right, but...

Do I understand you correct, that you want to map the request-parameter sort with the value totalAmount,desc to be available through the Sort object of the given Pageable object as amount (with order desc)?

Because of the names you mention in your post, I expect that you have Spring Data on the classpath.

IDKFS but maybe class SortHandlerMethodArgumentResolverCustomizer could help you to customize the SortHandlerMethodArgumentResolver the way, that you can map the request-parameter like you want to have.

If not, it should be possible to provide a @Bean of type SortHandlerMethodArgumentResolver that implements the behavior you want to have.

I'm not completely sure if the framework allows to override the specific part you need to be customized but it is IMO worth to have a look. :)