当前位置: 首页 > 知识库问答 >
问题:

使用RestTemplate解析JSON时的问题

弓明亮
2023-03-14

我试图使用restTemplate.exchange()方法解析来自服务的JSON响应。但由于解析错误而失败。

Java代码

        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());*
[
    {
        "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对象Java类

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;
            }

        }

    }

}

错误:

2018-07-12 15:40:00.876错误6044--[nio-8080-exec-1]O.A.C.C.C.[.[/].[dispatcherServlet]:路径为[]的上下文中servlet[dispatcherServlet]的servlet.Service()引发异常[请求处理失败;嵌套异常为org.springframework.web.client.RestClientException:提取类型[class com.example.demo.satchbresponse]和内容类型[application/JSON;charset=UTF-8]的响应时出错;嵌套异常为

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.example.demo.SATCHBResponse` out of

共有1个答案

壤驷俊逸
2023-03-14

tryresponseEntity >result=restTemplate.exchange(<>,HttpMethod.Get,entity,new ParameterizedTypeReference >(){});

 类似资料:
  • 问题内容: 我是使用AngularJS的新手,但在解析json响应时遇到问题。这是我正在使用的HTML代码: 这是使用AngularJS的Javascript代码: HTML使用服务器返回的JSON显示变量$ scope.response,但顶部列表中未显示任何内容。JSON的格式完美,但是$ scope.cars变量似乎始终为空。 我究竟做错了什么? 非常感谢, GA 问题答案: $ http.

  • 我是新来改装的。我已经在大多数Web服务中使用了volley,决定继续改进。但是我被Json解析所困,我在POJO类中得到了空数据。我尝试通过检查响应,我的响应是正确的,当我在模型类中解析时,我得到的是空白数据。请指导我,任何帮助都将不胜感激。 注意:POJO类是可parcelable,还包含cunstructor和gette/setter。我从这里删除了它,因为文本限制。 JSON响应

  • 问题内容: 我正在尝试在我的android应用程序中解析Json的链接是https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776 当我将其放入Json对象时,它给我带来错误错误是:08-31 14:40:52.281:WARN / Syst

  • 问题内容: 我正在用Python阅读JSON文件,其中包含许多字段和值(约8000条记录)。Env:Windows 10,Python 3.6.4;码: 这样我得到一个错误。下面是堆栈跟踪: 伴随着我,我尝试了 与此相关,我的程序运行了很长时间,然后挂起,没有任何输出。 我搜索了几乎与此相关的所有主题,但找不到解决方案。 注意:JSON数据是有效的,因为当我在Postman /任何REST客户端上

  • 问题内容: 我正在尝试解析相同的JSON,但是现在我对类进行了一些更改。 我的班级现在看起来像: 此代码引发异常, 可以理解,因为按照我上一个问题的解决方案,GSON希望将Enum对象实际创建为 但是,由于从语法上讲这是不可能的,因此推荐的解决方案和解决方法是什么? 问题答案: 从Gson的文档中: Gson为枚举提供了默认的序列化和反序列化…如果您想更改默认的表示形式,则可以通过GsonBuil

  • 这就是我如何尝试使用RestTemplate调用服务器, 这就是我如何使用它(用于测试) 我的登录响应类及其子类, null null null null 公共类BaseResponse{ 受保护字符串StatusCode; 我的问题是,1。为什么调用服务器时会出现此错误 信息:org.springframework.beans.factory.support.defaultListableBea