首页 文章

使用RestTemplate进行JSON解析的问题

提问于
浏览
0

我试图使用RestTemplate.exchange()方法解析服务中的JSON响应 . 但它失败了解析错误 .

Java code

RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setAccept (Arrays.asList(MediaType.APPLICATION_JSON));
        headers.set("Token", "********");
        System.out.println("***************************");
        System.out.println(headers.toString());
        HttpEntity<String> entity = new HttpEntity<String>("", headers);

        ResponseEntity<SATCHBResponse> result = restTemplate.exchange(<>, HttpMethod.GET, entity, SATCHBResponse.class);

        System.out.println(result.getBody());*

JSON Response from the HTTP call

[
    {
        "MetaData":  
            {
            "SPVersion":    "0" ,
            "Status":   "Informational" ,
            "Description":      "High Memory Usage; Hardware Failure detected"  ,
            "SchemaVesion":     1   ,
            "AgentVersion":     "2.0.0.0"   ,
            "ServiceTag":   "xxxxx" ,
            "Model":    "xxxxxxxxxxxx"  ,
            "OSVersion":    "6.3.9600"  ,
            "BIOSVersion":      "A08"   ,
            "UTCDate":      "2018-06-20T18:22:32.3388283Z"  ,
            "Registered":   false
            },
        "id":   "xxxxxxxxxxxxxxxxxxxxxxxx"  ,
        "Information":  
            {
            "SystemDriveFreeSpace":     "110.29 GB" ,
            "LastBootUpDate":   "2017-04-07T20:48:13.486816"    ,
            "HighCPUUsage":     60  ,
            "WindowsUpdatedRunDate":    "2017-04-05T08:13:42"   ,
            "VideoCard-ddqdwqdwqdwqC77VGACable":  
                {
                "Status":   "Warning"   ,
                "Type":     "PredictiveFailure" ,
                "Source":   "DDV"
                },
                "LocalDate":    "2018-06-10T23:52:32.3970051+05:30" ,
                "HighMemUsage":     87.5
                }
    },
    {
        "MetaData":  
            {
            "SPVersion":    "0" ,
            "Status":   "Informational" ,
            "Description":      "High Memory Usage; Hardware Failure detected"  ,
            "SchemaVesion":     1   ,
            "AgentVersion":     "2.0.0.0"   ,
            "ServiceTag":   "sddddfdsfd"    ,
            "Model":    "dfdsfsdfsdfdsf"    ,
            "OSVersion":    "6.3.9600"  ,
            "BIOSVersion":      "A08"   ,
            "UTCDate":      "2018-06-19T18:22:32.3388283Z"  ,
            "Registered":   false
            },
        "id":   "dfdsfsd$$9223370507470375807"  ,
        "Information":  
            {
            "SystemDriveFreeSpace":     "110.29 GB" ,
            "LastBootUpDate":   "2017-04-07T20:48:13.486816"    ,
            "HighCPUUsage":     60  ,
            "WindowsUpdatedRunDate":    "2017-04-05T08:13:42"   ,
            "VideoCard-Cadsfdfsdafds7VGACable":  
                {
                "Status":   "Warning"   ,
                "Type":     "PredictiveFailure" ,
                "Source":   "DDV"
                },
            "LocalDate":    "2018-06-10T23:52:32.3970051+05:30" ,
            "HighMemUsage":     87.5
            }
    }
]

JSON Object Java Class

package com.example.demo;


import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;



@JsonPropertyOrder({
    "topLevelArray"
})

public class SATCHBResponse {

    @JsonProperty("topLevelArray")
    protected List<SATCHBResponse.TopLevelArray> topLevelArray;

    public List<SATCHBResponse.TopLevelArray> getTopLevelArray() {
        if (topLevelArray == null) {
            topLevelArray = new ArrayList<SATCHBResponse.TopLevelArray>();
        }
        return this.topLevelArray;
    }

    public void setTopLevelArray(List<SATCHBResponse.TopLevelArray> value) {
        this.topLevelArray = value;
    }


    @JsonPropertyOrder({
        "metaData",        
        "information",
        "id"

    })
    public static class TopLevelArray {

        @JsonProperty("MetaData")
        protected SATCHBResponse.TopLevelArray.MetaData metaData;       
        @JsonProperty("Information")
        protected SATCHBResponse.TopLevelArray.Information information;
        @JsonProperty("id")
        protected String id;



        public SATCHBResponse.TopLevelArray.MetaData getMetaData() {
            return metaData;
        }


        public void setMetaData(SATCHBResponse.TopLevelArray.MetaData value) {
            this.metaData = value;
        }

        public SATCHBResponse.TopLevelArray.Information getInformation() {
            return information;
        }


        public void setInformation(SATCHBResponse.TopLevelArray.Information value) {
            this.information = value;
        }


        public String getId() {
            return id;
        }

        public void setId(String value) {
            this.id = value;
        }
//        "applicationCrashesLast24Hrs", 

        @JsonPropertyOrder({
            "localDate",
            "lastBootUpDate",
            "windowsUpdatedRunDate",
            "systemDriveFreeSpace",                       
            "highMemUsage",
            "highCPUUsage",
            "other71A5083EDC91460C956878CB755731AE"
        })
        public static class Information {

            @JsonProperty("LocalDate")
            protected String localDate;
            @JsonProperty("LastBootUpDate")
            protected String lastBootUpDate;
            @JsonProperty("WindowsUpdatedRunDate")
            protected String windowsUpdatedRunDate;
//            @JsonProperty("ApplicationCrashesLast24hrs")
//            protected String applicationCrashesLast24Hrs;
            @JsonProperty("SystemDriveFreeSpace")
            protected String systemDriveFreeSpace;
            @JsonProperty("HighMemUsage")
            protected Double highMemUsage;
            @JsonProperty("HighCPUUsage")
            protected String highCPUUsage;
            @JsonProperty("Other-71A5083E-DC91-460C-9568-78CB755731AE")
            protected SATCHBResponse.TopLevelArray.Information.Other71A5083EDC91460C956878CB755731AE other71A5083EDC91460C956878CB755731AE;


            public String getLocalDate() {
                return localDate;
            }


            public void setLocalDate(String value) {
                this.localDate = value;
            }


            public String getLastBootUpDate() {
                return lastBootUpDate;
            }


            public void setLastBootUpDate(String value) {
                this.lastBootUpDate = value;
            }


            public String getWindowsUpdatedRunDate() {
                return windowsUpdatedRunDate;
            }


            public void setWindowsUpdatedRunDate(String value) {
                this.windowsUpdatedRunDate = value;
            }


  /*          public String getApplicationCrashesLast24Hrs() {
                return applicationCrashesLast24Hrs;
            }


            public void setApplicationCrashesLast24Hrs(String value) {
                this.applicationCrashesLast24Hrs = value;
            }*/


            public String getSystemDriveFreeSpace() {
                return systemDriveFreeSpace;
            }


            public void setSystemDriveFreeSpace(String value) {
                this.systemDriveFreeSpace = value;
            }


            public Double getHighMemUsage() {
                return highMemUsage;
            }


            public void setHighMemUsage(Double value) {
                this.highMemUsage = value;
            }


            public String getHighCPUUsage() {
                return highCPUUsage;
            }


            public void setHighCPUUsage(String value) {
                this.highCPUUsage = value;
            }


            public SATCHBResponse.TopLevelArray.Information.Other71A5083EDC91460C956878CB755731AE getOther71A5083EDC91460C956878CB755731AE() {
                return other71A5083EDC91460C956878CB755731AE;
            }


            public void setOther71A5083EDC91460C956878CB755731AE(SATCHBResponse.TopLevelArray.Information.Other71A5083EDC91460C956878CB755731AE value) {
                this.other71A5083EDC91460C956878CB755731AE = value;
            }



            @JsonPropertyOrder({
                "source",
                "type",
                "status"
            })
            public static class Other71A5083EDC91460C956878CB755731AE {

                @JsonProperty("Source")
                protected String source;
                @JsonProperty("Type")
                protected String type;
                @JsonProperty("Status")
                protected String status;


                public String getSource() {
                    return source;
                }


                public void setSource(String value) {
                    this.source = value;
                }


                public String getType() {
                    return type;
                }


                public void setType(String value) {
                    this.type = value;
                }


                public String getStatus() {
                    return status;
                }


                public void setStatus(String value) {
                    this.status = value;
                }

            }

        }


        @JsonPropertyOrder({
            "schemaVersion",
            "agentVersion",
            "serviceTag",
            "model",
            "registered",
            "osVersion",
            "spVersion",
            "status",
            "utcDate",
            "biosVersion",            
            "description"
        })
        public static class MetaData {

            @JsonProperty("SchemaVersion")
            protected String schemaVersion;
            @JsonProperty("AgentVersion")
            protected String agentVersion;
            @JsonProperty("ServiceTag")
            protected String serviceTag;
            @JsonProperty("Model")
            protected String model;
            @JsonProperty("Registered")
            protected String registered;
            @JsonProperty("OSVersion")
            protected String osVersion;
            @JsonProperty("SPVersion")
            protected String spVersion;
            @JsonProperty("Status")
            protected String status;
            @JsonProperty("UTCDate")
            protected String utcDate;
            @JsonProperty("BIOSVersion")
            protected String biosVersion;            
            @JsonProperty("Description")
            protected String description;


            public String getSchemaVersion() {
                return schemaVersion;
            }


            public void setSchemaVersion(String value) {
                this.schemaVersion = value;
            }


            public String getAgentVersion() {
                return agentVersion;
            }


            public void setAgentVersion(String value) {
                this.agentVersion = value;
            }


            public String getServiceTag() {
                return serviceTag;
            }


            public void setServiceTag(String value) {
                this.serviceTag = value;
            }


            public String getModel() {
                return model;
            }


            public void setModel(String value) {
                this.model = value;
            }


            public String getRegistered() {
                return registered;
            }


            public void setRegistered(String value) {
                this.registered = value;
            }


            public String getOSVersion() {
                return osVersion;
            }


            public void setOSVersion(String value) {
                this.osVersion = value;
            }


            public String getSPVersion() {
                return spVersion;
            }


            public void setSPVersion(String value) {
                this.spVersion = value;
            }


            public String getUTCDate() {
                return utcDate;
            }


            public void setUTCDate(String value) {
                this.utcDate = value;
            }


            public String getBIOSVersion() {
                return biosVersion;
            }


            public void setBIOSVersion(String value) {
                this.biosVersion = value;
            }


            public String getStatus() {
                return status;
            }


            public void setStatus(String value) {
                this.status = value;
            }


            public String getDescription() {
                return description;
            }


            public void setDescription(String value) {
                this.description = value;
            }

        }

    }

}

Error

2018-07-12 15:40:00.876 ERROR 6044 --- [nio-8080-exec-1] oaccC [ . [ . [/] . [dispatcherServlet]:Servlet中的Servlet.service()[dispatcherServlet] with path []抛出异常[请求处理失败;嵌套异常是org.springframework.web.client.RestClientException:提取类型[class com.example.demo.SATCHBResponse]和内容类型[application / json; charset = UTF-8]的响应时出错;嵌套异常是org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法从START_ARRAY标记中反序列化com.example.demo.SATCHBResponse的实例;嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException:无法在[Source:(PushbackInputStream);中的START_ARRAY标记中反序列化com.example.demo.SATCHBResponse的实例 . line:2,column:2]] with root cause com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化com.example.demo.SATCHBResponse的实例
[Source:(PushbackInputStream)的START_ARRAY标记;在com.fasterxml的com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)〜[jackson-databind-2.9.6.jar:2.9.6]的第2行,第2列:第2行 . jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)〜[jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138)~ [jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092)〜[jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1461)〜[jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.deser.BeanDeserializer . _deserializeOther(BeanDeserializer.java:185)~ [jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)~ [jackson-dat abind-2.9.6.jar:2.9.6] com的com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)〜[jackson-databind-2.9.6.jar:2.9.6] . 在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java)中的fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084)〜[jackson-databind-2.9.6.jar:2.9.6] :235)〜[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:223)~ [spring-web- 5.0.7.RELEASE.jar:5.0.7.RELEASE]在org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100)〜[spring-web-5.0.7.RELEASE.jar:5.0.7 .RELEASE] org.springframework.web.client.RestTemplate $ responseEntityResponseExtractor.extractData(RestTemplate.java:991)〜[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] org.springframework.web .client.RestTemplate $ RES org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725)中的ponseEntityResponseExtractor.extractData(RestTemplate.java:974)〜[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]〜 [spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)〜[spring-web-5.0.7.RELEASE . jar:5.0.7.RELEASE]在com的org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)〜[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] . 示例.demo.RestConsumerNewApplication.getHBDetails(RestConsumerNewApplication.java:49)〜[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_171] at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_171] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_171] at java.lang.reflect.Method.invoke(Unknown Source)〜[na:1.8 .0_171] org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod)的.java:209)

1 回答

  • 0

    试试 ResponseEntity<List<TopLevelArray>> result = restTemplate.exchange(<>, HttpMethod.GET, entity, new ParameterizedTypeReference<List<TopLevelArray>>() {});

相关问题