我有一个服务通过rest模板调用另一个Api,并且该Api返回ResponseEntity对象。我尝试了一些方法,但无法获取所需的数据。
这是我调用的API的代码
SurveyService.java
public ResponseEntity<Object> getSurveyData(String searchText) {
List<SurveyResponse> dataList = new ArrayList<>();
List<Map<Object, Object>> results = someRepo.getSurveyData(searchText);
if(null != results) {
for (Map<Object, Object> item : results) {
Survey t = (Survey) ObjectMapper.convertMapToJson(item, Survey.class); //Utility class
SurveyResponse response = SurveyResponse.builder()
.id(t.getId())
.type(t.getType())
.name(t.getName())
.jobDesc(t.getJobDesc())
.build();
dataList.add(response);
}
}
return ResponseHandler.generateResponse(SUCCESS, HttpStatus.OK, dataList);
}
下面是实用程序响应处理程序类
ResponseHandler.java
public static ResponseEntity<Object> generateResponse(String message, HttpStatus status, Object responseObj) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put(STATUS, status.value());
map.put(MESSAGE, message);
map.put(DATA, responseObj);
return new ResponseEntity<>(map, status);
}
这是实体类
Survey.java
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Survey {
@Id
private String id;
private String type;
private String name;
private String jobDesc;
}
Api 的响应如下所示
{
"status": 200,
"message": "SUCCESS",
"data": [
{
"id": "12345",
"type": "person",
"name": "P1",
"jobDesc":"QA"
}
]
}
这是我的 Service 类中的代码,我创建了一个名为 Survey.java 的实体类来映射来自 Api 的响应
PersonSurveyService.java
public void getSurvey(String personId){
try {
ResponseEntity<Map<String, Object>> response = restTemplate.exchange(url+personId, HttpMethod.GET, null,
new ParameterizedTypeReference<Map<String, Object>>() {
});
List<Survey> alist = (List<Survey>) response.getBody().get("data");
System.out.println("checking first element"+alist.get(0).getId());
}
catch(Exception ex) {
System.out.println("Exception is:"+ex);
}
上面的 sysout 打印得很好,直到我打印 alist.get(0) 但当我添加 getId() 时它会抛出以下异常。
Exception is:java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.test.entity.Survey (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.test.entity.Survey is in unnamed module of loader 'app')
我尝试了一些其他方法
Approach 1:
ResponseEntity<Survey> response = restTemplate.getForEntity(url + personId, Survey.class);
I do not get any error here but all the fields in Survey object gets mapped as null.
Approach 2:
ResponseEntity<Map<String, Object>> response = restTemplate.exchange(url+personId, HttpMethod.GET, null,
new ParameterizedTypeReference<Map<String, Object>>() {
});
objectMapper.convertValue(response.getBody(),Survey.class);
Here I get an error: Exception is:java.lang.IllegalArgumentException: Unrecognized field "status"
有关如何从下面的响应对象中正确获取详细信息(id、名称等)的任何输入。
{
"status": 200,
"message": "SUCCESS",
"data": [
{
"id": "12345",
"type": "person",
"name": "P1",
"jobDesc":"QA"
}
]
}
使用 ResponseEntity 作为 API 的返回类型,并在使用 restemplate 调用它时使用 ObjectNode.class 作为返回类类型。
您可以在生成响应方法中使用它:
ObjectNode nodes = new ObjectMapper().createObjectNode();
nodes.put(STATUS, status.value());
nodes.put(MESSAGE, message);
nodes.put(DATA, responseObj);
return ResponseEntity.status(HttpStatus.OK).body(nodes);
使用剩余模板调用它时,您可以使用如下所示的内容:
ResponseEntity<Map<String, Object>> response = restTemplate.exchange(url+personId, HttpMethod.GET, null, ObjectNode.class);
请尝试一下,让我知道它是否有帮助,否则我们将改变一些事情。