problem:
Response type is due to health Health node class, and class needs to be performed due to the health condition is not fixed, the return type of the data field is not fixed, so the use of @JsonAngGetter annotations, as JSON parsing the structure became very inconvenient.
aims:
Analytical data structure Json not standardized, information acquired only exception description.
Solution:
The following example
package com.zjs.password.health; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import java.util.HashMap; import java.util.Map; /** * @author 李文 * @create 2019-05-29 15:53 **/ public class jsonTest { @Data public static class HealthResponse { private String status; private Map<String, HealthData> other = new HashMap<>(); public String getStatus() { return status; } public HealthResponse setStatus(String status) { this.status = status; return this; } @JsonAnyGetter public Map<String, HealthData> getOther() { return other; } @JsonAnySetter public void setOther(String key, HealthData value) { this.other.put(key, value); } } @Data @JsonIgnoreProperties(ignoreUnknown = true) public static class HealthData { String status; String error; } public static void main(String[] args) throws Exception { ObjectMapper o = new ObjectMapper(); String sad = "{\"status\":\"DOWN\",\"custom\":{\"status\":\"UP\"},\"test\":{\"status\":\"UP\"},\"rabbit\":{\"status\":\"UP\",\"version\":\"3.7.7\"},\"db\":{\"status\":\"DOWN\",\"error\":\"MySQL****\"},\"hystrix\":{\"status\":\"UP\"}}"; HealthResponse healthResponse = o.readValue(sad, HealthResponse.class); StringBuilder builder=new StringBuilder(8); if (!"UP".equals(healthResponse.getStatus())) { for (Map.Entry<String, HealthData> s : healthResponse.getOther().entrySet()) { if (!"UP".equals(s.getValue().getStatus())) { builder.append(s.getKey()).append(" : ").append(s.getValue().getError()).append(" "); } } } System.out.println(builder.toString()); System.out.println(healthResponse.toString()); } }