gpt4 book ai didi

json - RestTemplate getForObject 无法映射自定义类

转载 作者:行者123 更新时间:2023-12-02 20:23:48 25 4
gpt4 key购买 nike

自定义类结构 DocumentListVO 与 JSON 响应一对一映射,但是在使用 DocumentListVO dv1 =restTemplate.getForObject(uri,DocumentListVO.class) 时,它会抛出以下堆栈跟踪错误:

GET request for
"http://aaa.ddd.com:8081/bpi1/service/aa/documen1t/list23"
resulted in 200 ()
[2018-05-28 12:49:46,397]-DEBUG org.springframework.web.client.RestTemplate - Reading [class
com.fascorp.isis.ejb.documentVO.DocumentListVO1] as
"application/json;charset=UTF-8" using
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@58206f8]
[2018-05-28 12:49:46,399]-DEBUG org.apache.http.wire - http-outgoing-0 << "0[\r][\n]"
[2018-05-28 12:49:46,400]-DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
[2018-05-28 12:49:46,402]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager -
Connection [id: 0][route: {}->http://aaa.ddd.com:8081] can be kept
alive indefinitely
[2018-05-28 12:49:46,404]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager -
Connection released: [id: 0][route:
{}->http://aaa.ddd.com:8081][total kept alive: 1; route allocated:
1 of 2; total allocated: 1 of 10]
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Unrecognized field "Results" (class
com.fascorp.isis.ejb.documentVO.DocumentListVO1), not marked as
ignorable (3 known properties: , "success", "results", "count"])
at [Source: org.apache.http.conn.EofSensorInputStream@209152d; line: 1, column: 13] (through reference chain:
com.fascorp.isis.ejb.documentVO.DocumentListVO1["Results"]); nested
exception is
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "Results" (class
com.fascorp.isis.ejb.documentVO.DocumentListVO1), not marked as
ignorable (3 known properties: , "success", "results", "count"])
at [Source: org.apache.http.conn.EofSensorInputStream@209152d; line: 1, column: 13] (through reference chain:
com.fascorp.isis.ejb.documentVO.DocumentListVO1["Results"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:527)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:259)
public class DocumentListVO 
{
String Count;
boolean Success;
Results[] Results;

public DocumentListVO()
{
super();
}

public DocumentListVO(String Count, boolean success, Results[] results)
{
super();
this.Count = Count;
this.Success = success;
this.Results = results;
}


public Results[] getResults()
{
return Results;
}
public void setResults( Results[] results)
{
this.Results = results;
}
public String getCount()
{
return Count;
}
public void setCount( String count)
{
this.Count = count;
}
public boolean isSuccess()
{
return Success;
}
public void setSuccess( boolean success)
{
this.Success = success;
}

@Override
public String toString()
{
return "DocumentListVO [count=" + Count + ", success=" + Success
+ ", results=" + Arrays.toString(Results) + "]";
}
}

JSON 响应:

{
"Results": [
{
"Category": "abcde",
"DateCreated": "12-13-2016 14:43:31",
"Token": "ad",
"ClassDescription": "Wisd",
"DocumentTitle": "Rela",
"ContentSize": "58",
"MimeType": "application/pdf"
},
{
"Category": "tyreytr",
"DateCreated": "11-07-2017 13:37:11",
"Token": "hgdf",
"ClassDescription": "With",
"DocumentTitle": "Related Doc - 1234646315",
"ContentSize": "58",
"MimeType": "application/pdf"
}
]
"Count": 89,
"Success": true
}

最佳答案

在您的字段上添加 @JsonProperty 注释。

public class DocumentListVO 
{

@JsonProperty("Count")
String Count;

@JsonProperty("Success")
boolean Success;

@JsonProperty("Results")
Results[] Results;

..... setters / getters
// add annotation on setter / getter too (not necessary)

}

原因是 Jackson 不读取私有(private)变量名称,而是读取 setter/getter 方法名称。
因此,Results 的 setter 将是 setResults,但 Jackson 根据 setter/getters 的命名约定将字段名称解释为 results
添加 @JsonProperty 后,您还可以按照 java 命名约定重命名字段,countsuccessresults等等

关于json - RestTemplate getForObject 无法映射自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50561443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com