Java 定义返回列表的REST端点
如何从控制器类返回列表 我当前的代码:Java 定义返回列表的REST端点,java,json,spring,hibernate,api,Java,Json,Spring,Hibernate,Api,如何从控制器类返回列表 我当前的代码: @PostMapping(value = Endpoint.RRESOURCE_customer_ID) public ResponseEntity<customerDto> getLocationsByLocationIds(@RequestBody @Validated @RequestParam(name = "ids", required = true) List<Long> ids) { List<cus
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
public ResponseEntity<customerDto> getLocationsByLocationIds(@RequestBody @Validated @RequestParam(name = "ids", required = true) List<Long> ids) {
List<customerDto> customerListDto = customerService.findcustomerIds(ids);
return ResponseEntity.ok(customerListDto);
}
我不确定下一个定义
public ResponseEntity<customerDto> getLocationsByLocationIds(@RequestBody @Validated @RequestParam(name ="ids", required = true) List<Long> ids)
您应该按如下方式定义端点:
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
public List<CustomerDto> getLocationsByLocationIds(@RequestBody List<Long> ids) {
return customerService.findcustomerIds(ids);
}
请注意,不能将@RequestBody和@RequestParam都放在同一字段上。该字段是HTTP请求正文或HTTP请求参数。您必须在ReponseEntity类中返回customerDto列表
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
@ResponseBody
public ResponseEntity<List<customerDto> > getLocationsByLocationIds(@RequestParam List<Long> ids) {
List<customerDto> customerListDto = customerService.findcustomerIds(ids);
return ResponseEntity.ok(customerListDto);
}
如何使用ResponseBy向该方法发送请求?我只想用RestTemplate发送一个客户id的long列表,如[1,3,15]。你可以在这里看到例子
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
public List<CustomerDto> getLocationsByLocationIds(@RequestBody List<Long> ids) {
return customerService.findcustomerIds(ids);
}
@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
@ResponseBody
public ResponseEntity<List<customerDto> > getLocationsByLocationIds(@RequestParam List<Long> ids) {
List<customerDto> customerListDto = customerService.findcustomerIds(ids);
return ResponseEntity.ok(customerListDto);
}