how to configure spring-data-rest search method path with @PathVariable

别等时光非礼了梦想. 提交于 2019-12-01 09:45:29

问题


I want to customize my spring-data-rest search method path by passing parameter as a path variable like follows

http://localhost:8080/orders/search/customers/{customerId}

findByCustomer(@PathVariable("customerId") Integer customer);

The search resource listh the links as follows

http://localhost:8080/orders/search/customers/%7BcustomerId%7D

How to expose search url with path params?


回答1:


You can use custom handler similar to this:

@RepositoryRestController
public class OrderController {

    @Autowired
    OrderRepository orderRepository;

    @GetMapping("/orders/search/customers/{id}")
    public @ResponseBody ResponseEntity<?> getByCustomers(@PathVariable Integer customer) {
        Order order = orderRepository.findOne(id);
        if(order == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        Resource<Order> resource = new Resource<Order>(order); 
        return ResponseEntity.ok(resource);
    }
}

More about this can be found here.




回答2:


Use HttpServletRequest to get the request url:

findByCustomer(@PathVariable("customerId") Integer customer, HttpServletRequest request){
    String request = request.getRequestURL().toString(); // StringBuffer, so use append if you want to...
    [...]
}

also you can use request.getQueryString() to get the query part after ?.



来源:https://stackoverflow.com/questions/39866926/how-to-configure-spring-data-rest-search-method-path-with-pathvariable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!