使用 Jackson 解析响应
Parse response using Jackson
我正在尝试使用 Jackson 解析 api 响应。收到类似 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 的错误:无法识别的字段 "Health"
我试过
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); //with true
我认为是简单的错误,但无法弄清楚。请帮忙
响应json:
{
"Health": {
"id": "abc_Server",
"name": "ABC Request Service",
"status": "GREEN",
"dependencies": [
{
"id": "DB",
"name": "MySQL",
"message": "Connection successful.",
"status": "GREEN"
}
]
}
}
java pojos
@JsonRootName(value = "Health")
public class HealthResponse {
private String id;
private String name;
private String status;
private List<Dependencies> dependencies;
//getter and setter methods
}
}
public class Dependencies {
private String id;
private String name;
private String message;
private String status;
//getter and setter methods
}
主要class:
ObjectMapper objectMapper = new ObjectMapper();
try {
InputStream response = healthCheckWebTarget.request(MediaType.APPLICATION_JSON).get(InputStream.class);
HealthResponse healthResponse = objectMapper.readValue(response, HealthResponse.class);
}catch(Exception e){
//
}
也尝试过使用 pojo 但没有成功
@JsonRootName(value = "Health")
public class Health {
private HealthResponse health;
//getter and setter
}
当将 JSON 转换为 Java 对象时,您实际上是在反序列化,而不是序列化。所以使用这个:
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
目前为我工作的完整代码(打印 abc_Server):
String json="{\"Health\":{\"id\":\"abc_Server\",\"name\":\"ABCRequestService\",\"status\":\"GREEN\",\"dependencies\":[{\"id\":\"DB\",\"name\":\"MySQL\",\"message\":\"Connectionsuccessful.\",\"status\":\"GREEN\"}]}}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
try {
HealthResponse healthResponse = objectMapper.readValue(json, HealthResponse.class);
System.out.println(healthResponse.getId());
} catch (Exception e) {
e.printStackTrace();
}
和HealthResponse
:
@JsonRootName(value = "Health")
class HealthResponse {
[...]
}
Dependencies
无变化
UNWRAP_ROOT_VALUE 的文档。
我正在尝试使用 Jackson 解析 api 响应。收到类似 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 的错误:无法识别的字段 "Health"
我试过
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); //with true
我认为是简单的错误,但无法弄清楚。请帮忙
响应json:
{
"Health": {
"id": "abc_Server",
"name": "ABC Request Service",
"status": "GREEN",
"dependencies": [
{
"id": "DB",
"name": "MySQL",
"message": "Connection successful.",
"status": "GREEN"
}
]
}
}
java pojos
@JsonRootName(value = "Health")
public class HealthResponse {
private String id;
private String name;
private String status;
private List<Dependencies> dependencies;
//getter and setter methods
}
}
public class Dependencies {
private String id;
private String name;
private String message;
private String status;
//getter and setter methods
}
主要class:
ObjectMapper objectMapper = new ObjectMapper();
try {
InputStream response = healthCheckWebTarget.request(MediaType.APPLICATION_JSON).get(InputStream.class);
HealthResponse healthResponse = objectMapper.readValue(response, HealthResponse.class);
}catch(Exception e){
//
}
也尝试过使用 pojo 但没有成功
@JsonRootName(value = "Health")
public class Health {
private HealthResponse health;
//getter and setter
}
当将 JSON 转换为 Java 对象时,您实际上是在反序列化,而不是序列化。所以使用这个:
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
目前为我工作的完整代码(打印 abc_Server):
String json="{\"Health\":{\"id\":\"abc_Server\",\"name\":\"ABCRequestService\",\"status\":\"GREEN\",\"dependencies\":[{\"id\":\"DB\",\"name\":\"MySQL\",\"message\":\"Connectionsuccessful.\",\"status\":\"GREEN\"}]}}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
try {
HealthResponse healthResponse = objectMapper.readValue(json, HealthResponse.class);
System.out.println(healthResponse.getId());
} catch (Exception e) {
e.printStackTrace();
}
和HealthResponse
:
@JsonRootName(value = "Health")
class HealthResponse {
[...]
}
Dependencies
无变化
UNWRAP_ROOT_VALUE 的文档。