【问题标题】:Define REST endpoint that returns a List定义返回列表的 REST 端点
【发布时间】:2018-10-05 12:05:38
【问题描述】:

如何从控制器类返回列表?

我当前的代码:

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

【问题讨论】:

    标签: java json spring hibernate api


    【解决方案1】:

    您应该如下定义您的端点:

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

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

    【讨论】:

    • 如何使用 responsebody 向该方法发送请求?我只想为客户的 id 发送一个 long 列表,例如 ["1","3","15"]
    • RestTemplate。您可以在这里查看示例baeldung.com/rest-template
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多