abidinberkay
abidinberkay

Reputation: 2025

Define REST endpoint that returns a List

How can I return a list from a controller class?

My current code:

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

The error I receive:

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>

The findcustomerIds method:

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

I am not sure about the next definition.

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

Upvotes: 0

Views: 162

Answers (2)

Fernix
Fernix

Reputation: 371

You have to return a List of "customerDto" in your ReponseEntity class

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

Upvotes: 0

user10367961
user10367961

Reputation:

You should define your endpoint as follows:

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

Note that you can't have both @RequestBody and @RequestParam on the same field. The field is either the HTTP Requests body or a HTTP Request parameter.

Upvotes: 1

Related Questions