[英]Define REST endpoint that returns a List
如何從控制器類返回列表?
我當前的代碼:
@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)
您應該如下定義端點:
@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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.