java Spring:RestTemplate 返回空对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41149412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: RestTemplate returns null object
提问by Spring
With the below GET request:
使用以下 GET 请求:
ResponseEntity<String> entity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class );
entity.getBody();
returns a JSON String like this:
返回这样的 JSON 字符串:
{"userRegistrations":[{"userRegistrationToken":"fb398972","userRegistrationTokenAlias":"87f15f8"}]}
But I want to make this work with an object not with a string. So with the code below I receive a UserRegistrations object with a null UserTokenResponse List
但我想用一个对象而不是字符串来完成这项工作。因此,使用下面的代码,我收到一个 UserRegistrations 对象,其中包含一个空 UserTokenResponse 列表
ResponseEntity<UserRegistrations> entity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, UserRegistrations.class );
entity.getBody();
And my domain class looks like this:
我的域类如下所示:
public class UserRegistrations {
List<UserTokenResponse> userRegistrationList;
//..getters and setters
}
public class UserTokenResponse {
private String userRegistrationToken;
private String userRegistrationTokenAlias;
//getters and setters
}
What am I missing?
我错过了什么?
采纳答案by JB Nizet
Assuming you're using Hymanson, RestTemplate
automatically registers a MappingHymanson2HttpMessageConverter
which configures the underlying ObjectMapper
to ignore unknown properties.
假设您正在使用 Hymanson,RestTemplate
自动注册一个MappingHymanson2HttpMessageConverter
配置底层ObjectMapper
以忽略未知属性。
The JSON object has a single attribute named userRegistrations
, whereas your Java class has a single attribute named userRegistrationList
. They don't match.
JSON 对象有一个名为 的属性userRegistrations
,而您的 Java 类有一个名为 的属性userRegistrationList
。他们不匹配。
They need to match, or you need to add a @JsonProperty
annotation of the attribute to make Hymanson serialize/parse it as userRegistrations
.
它们需要匹配,或者您需要添加@JsonProperty
属性的注释以使 Hymanson 将其序列化/解析为userRegistrations
.
回答by Kokkonda Abhilash
This happens when your class property names doesn't match with the JSON property names coming in the response. For instance take the below example
当您的类属性名称与响应中的 JSON 属性名称不匹配时,就会发生这种情况。例如下面的例子
public class ScheduledCallbacks {
private List<Callback> callbacks;
public List<Callback> getCallbacks() {
return callbacks;
}
public void setCallbacks(List<Callback> callbacks) {
this.callbacks = callbacks;
}
@Override
public String toString() {
return "ScheduledCallbacks [callbacks=" + callbacks + "]";
}
}
and if the response is the following way
如果响应是以下方式
{
"scheduledCallbacks": [
{
"sessionId": "string",
"customerNbr": "string",
"topicName": "string",
"desiredTime": "string",
"callbackState": "string",
"serviceName": "string",
"expirationTime": "string",
"programCode": "string"
}
]
}
Then you get null response because the name scheduledCallbacks in the JSON response doesn't match with the name callbacks in class.
然后你会得到空响应,因为 JSON 响应中的名称 scheduleCallbacks 与类中的名称回调不匹配。
But if your class is as following
但是如果你的班级如下
public class ScheduledCallbacks {
private List<Callback> scheduledCallbacks;
public List<Callback> getScheduledCallbacks() {
return scheduledCallbacks;
}
public void setScheduledCallbacks(List<Callback> scheduledCallbacks) {
this.scheduledCallbacks = scheduledCallbacks;
}
@Override
public String toString() {
return "ScheduledCallbacks [scheduledCallbacks=" + scheduledCallbacks + "]";
}
}
Then you get the expected response in response entity
然后你在响应实体中得到预期的响应