2016-10-05 57 views
0

我想通過傳遞參數像一個路徑變量定製我的彈簧數據休息搜索方法路徑彈簧數據休息搜索方法路徑遵循如何配置@PathVariable

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

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

搜索資源listh鏈接如下

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

如何使路徑參數暴露搜索網址?

+0

你是通過customerId的價值? – Nimesh

+0

customerId是一個整數取值爲1 – SST

+0

對不起,我沒有得到你想要做的。你能提供更多的信息嗎? – Patrick

回答

0

使用HttpServletRequest獲得請求的URL:

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

也可以使用request.getQueryString()?後得到的查詢部分。

1

您可以使用類似這樣的自定義處理程序:

@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); 
    } 
} 

更多關於這可以發現here