【问题标题】:how to fix a response entity object returning null in its body如何修复在其主体中返回 null 的响应实体对象
【发布时间】:2019-09-17 21:31:33
【问题描述】:

如何修复在正文中返回 null 的休息模板对象

ResponseEntity<List<Spot>> reEntity = new ResponseEntity<List<Spot>>(HttpStatus.OK);
        ParameterizedTypeReference<List<Spot>> parameterizedTypeReference =  new ParameterizedTypeReference<List<Spot>>() {
        };
List<Spot> spots = new ArrayList<Spot>(); //initialized
resEntity.getBody().addAll(spots);//getBody() is  returning null

【问题讨论】:

  • 你能展示你如何初始化spots变量吗?
  • @itwasntme 我已更新并显示

标签: java spring http resttemplate


【解决方案1】:

你应该在这一行定义正文

ResponseEntity<List<Spot>> reEntity = new ResponseEntity<List<Spot>>(HttpStatus.OK);

喜欢下一行

ResponseEntity<List<Spot>> reEntity = new ResponseEntity<List<Spot>>(spots, HttpStatus.OK);

我什至建议您使用 ResponseEntity 构建器,如下所示

ResponseEntity.ok(spots);

【讨论】:

  • 它不起作用。为第二行写了什么 ParameterizedTypeReference> parameterizedTypeReference = new ParameterizedTypeReference>() { };
  • 哪里需要使用parameterizedTypeReference?它存在的原因是什么?
  • 现在声明和初始化它的方式没有在任何地方使用
猜你喜欢
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多