简体   繁体   English

Spring:RestTemplate 返回空对象

[英]Spring: RestTemplate returns null object

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?我错过了什么?

Assuming you're using Jackson, RestTemplate automatically registers a MappingJackson2HttpMessageConverter which configures the underlying ObjectMapper to ignore unknown properties.假设您使用的是 Jackson, RestTemplate自动注册一个MappingJackson2HttpMessageConverter ,它将底层ObjectMapper配置为忽略未知属性。

The JSON object has a single attribute named userRegistrations , whereas your Java class has a single attribute named userRegistrationList . JSON 对象有一个名为userRegistrations属性,而您的 Java 类有一个名为userRegistrationList属性。 They don't match.他们不匹配。

They need to match, or you need to add a @JsonProperty annotation of the attribute to make Jackson serialize/parse it as userRegistrations .它们需要匹配,或者您需要添加属性的@JsonProperty注释,以使 Jackson 将其序列化/解析为userRegistrations

This happens when your class property names doesn't match with the JSON property names coming in the response.当您的类属性名称与响应中的 JSON 属性名称不匹配时,就会发生这种情况。 For instance take the below example例如下面的例子

    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然后你在响应实体中得到预期的响应

I encountered a similar error and it was returning null too.我遇到了类似的错误,它也返回 null。 The problem is over when Object.class is replaced with the name of the class we want to convert on the client side.当 Object.class 替换为我们要在客户端转换的类的名称时,问题就结束了。

Like that:像那样:

Token = restTemplate.exchange(uri, HttpMethod.POST, request, Object.class);

the problem was probably due to the fact that it is not directly compatible with the class we want to convert.问题可能是由于它与我们要转换的类不直接兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM