Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定义返回列表的REST端点_Java_Json_Spring_Hibernate_Api - Fatal编程技术网

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