简体   繁体   English

Jackson-将JSON反序列化为类

[英]Jackson - Deserializing JSON to class

I am calling an endpoint which returns JSON that that looks something like this (in Postman): 我正在调用一个端点,该端点返回看起来像这样的JSON(在Postman中):

{
    "Result": {
        "attribute1": { ... },
        "attribute2": { ... }
    }
}

The Content-Type header returned by this request is text/x-json (as opposed to the usual application/json ). 此请求返回的Content-Type标头是text/x-json (与通常的application/json相反)。 I think this is causing some problems when trying to deserialize this through Jackson. 我认为这在尝试通过Jackson进行反序列化时会引起一些问题。 The POJO for this JSON looks something like this: 这个JSON的POJO看起来像这样:

@Getter
@Setter
public class Response {

    @JsonProperty("Result")
    private Result result;

}

The Result class is from an external library (the same guys who wrote this endpoint). Result类来自外部库(编写此终结点的人相同)。 Either ways, when I try to call this endpoint through RestTemplate.exchange() , Jackson is unable to deserialize this JSON into a valid Result class. 无论哪种方式,当我尝试通过RestTemplate.exchange()调用此终结RestTemplate.exchange() ,Jackson都无法将此JSON反序列化为有效的Result类。 I am doing this: 我正在这样做:

ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.GET, null, Response.class);

Doing response.getBody() gives a Response object which contains a null Result object. 进行response.getBody()会给出一个Response对象,其中包含一个空Result对象。 Apparently, Jackson is not deserializing the JSON properly. 显然,Jackson没有正确反序列化JSON。 I suspect this is because of the unusual text/x-json Content-Type returned by the API. 我怀疑这是由于API返回的异常text/x-json Content-Type。

I also have my MappingJackson2HttpMessageConverter object configured to be able to parse text/x-json Content-type, but no luck: 我也将我的MappingJackson2HttpMessageConverter对象配置为能够解析text/x-json Content-type,但是没有运气:

MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setSupportedMediaTypes(ImmutableList.of(new MediaType("text", "x-json")));
restTemplate.getMessageConverters().add(jsonConverter);

Any pointers? 有指针吗?

Update: I don't know why this didn't work, but I figured out an alternative way - fetching the JSON as Map instead of a domain object, which is good enough for my purposes. 更新:我不知道为什么这不起作用,但是我想出了另一种方法-将JSON作为Map而不是域对象来获取,这足以满足我的目的。

By default MappingJackson2HttpMessageConverter is bind to: 默认情况下, MappingJackson2HttpMessageConverter绑定到:

  • application/json 应用程序/ JSON
  • application/*+json 应用/ * + JSON

We need to add text/x-json . 我们需要添加text/x-json

MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
List<MediaType> jsonTypes = new ArrayList<>(jsonConverter.getSupportedMediaTypes());
jsonTypes.add(new MediaType("text", "x-json"));
jsonConverter.setSupportedMediaTypes(jsonTypes);

Now, we should use it in RestTemplate : 现在,我们应该在RestTemplate使用它:

restTemplate.setMessageConverters(Collections.singletonList(jsonConverter));
ResponseEntity<RequestPayload> response = restTemplate.exchange(url, HttpMethod.GET, null, RequestPayload.class);

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

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