定义返回列表的REST端点

时间:2018-10-05 12:05:39

标签: 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<customerDto> customerListDto = customerService.findcustomerIds(ids);
      return ResponseEntity.ok(customerListDto);
}

我收到的错误:

Error:(51, 41) java: incompatible types: inference variable T has incompatible bounds
    equality constraints: com.testclass.cust.common.dto.output.CustomerDto
    lower bounds: java.util.List<com.customer.common.dto.output.CustomerDto>

findcustomerIds方法:

@Transactional
public List<customerDto> findcustomerIds(List<Long> customerIds) {
    List<customer> customerList = repository.findAll(customerIds);
    return mapper.mapAsList(customerList, customerDto.class);
}

我不确定下一个定义。

public ResponseEntity<customerDto> getLocationsByLocationIds(@RequestBody @Validated @RequestParam(name ="ids", required = true) List<Long> ids)

2 个答案:

答案 0 :(得分:1)

您应按以下方式定义端点:

@PostMapping(value = Endpoint.RRESOURCE_customer_ID)
public List<CustomerDto> getLocationsByLocationIds(@RequestBody List<Long> ids) {
    return customerService.findcustomerIds(ids);
}

请注意,您不能在同一字段上同时使用@RequestBody@RequestParam。该字段是 HTTP请求正文或HTTP请求参数。

答案 1 :(得分:0)

您必须在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);
}