Java:使用“@ class”将其中的json反序列化为rest模板中的object在json -SpringBoot中 [英] Java: Deserialize a json to object in rest template using "@class" in json -SpringBoot

查看:842
本文介绍了Java:使用“@ class”将其中的json反序列化为rest模板中的object在json -SpringBoot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用@class中的信息实例化一个从JSON扩展抽象类的类,如下所示。

I have to instantiate a class which extends the abstract class from JSON using information in @class as shown below.

"name": {
  "health": "xxx",
  "animal": {
    "_class": "com.example.Dog",
    "height" : "20"
    "color" : "white"
  }
},

这里抽象类是动物,狗延伸动物类。因此,使用@class中的信息,我们可以直接实例化狗。这也是我在restTemplate中得到的响应

Here the abstract class is animal and dog extends the animal class. So using the information in @class, can we instantiate dog directly. Also this is the response I am getting in restTemplate

ResponseEntity<List<SomeListName>> response = restTemplate.exchange("http://10.150.15.172:8080/agencies", HttpMethod.GET, request, responseType);

执行此行时会出现以下错误。
由于POJO类是自动生成的,我不能使用注释,如 @JsonTypeInfo

The following error is coming when this line is executed. Since the POJO classes are auto-generated, I cannot use annotations like @JsonTypeInfo

我正在使用Spring启动和maven。
此错误将在控制台中出现。

I am using Spring boot and maven. This error is coming in console.


无法读取JSON:无法构造MyPOJO的实例,问题:摘要类型需要映射到具体类型,具有自定义反序列化器,或者使用其他类型信息进行实例化

Could not read JSON: Can not construct instance of "MyPOJO", problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information


推荐答案

您可以使用 @JsonTypeInfo 注释,无论通过遵守 MixIn的

You can use @JsonTypeInfo annotation regardless of the fact that the classes are generated, by adhering to MixIn's


混合注释是:一种将注释与注释相关联的方法
类,不修改(目标)类本身。

"mix-in annotations are": a way to associate annotations with classes, without modifying (target) classes themselves.

也就是说,你可以:

定义混合类(或接口)的注释将使用
与目标类(或接口),使得它看起来好像
目标类具有混合类的所有注释有(f或
配置序列化/反序列化的目的)

Define that annotations of a mix-in class (or interface) will be used with a target class (or interface) such that it appears as if the target class had all annotations that the mix-in class has (for purposes of configuring serialization / deserialization)

所以你可以写你的 AnimalMixIn class,类似于

So you can write your AnimalMixIn class, something like

@JsonTypeInfo(  
    use = JsonTypeInfo.Id.NAME,  
    include = JsonTypeInfo.As.PROPERTY,  
    property = "_class")  
@JsonSubTypes({  
    @Type(value = Cat.class, name = "com.example.Cat"),  
    @Type(value = Dog.class, name = "com.example.Dog") })  
abstract class AnimalMixIn  
{  

}  

并配置您的反序列化器

    ObjectMapper mapper = new ObjectMapper();  
    mapper.getDeserializationConfig().addMixInAnnotations(  
    Animal.class, AnimalMixIn.class);

由于您使用的是Spring Boot,因此您可以查看以下博文,了解如何自定义用于使用MixIns的ObjectMapper,自定义Jackson Object Mapper ,特别注意 mixIn 方法 Jackson2ObjectMapperBuilder

Since you're using Spring Boot, you can check the following blog post to see how you can customize the ObjectMapper for using the MixIns, Customizing the Jackson Object Mapper, note especially the mixIn method of Jackson2ObjectMapperBuilder

RestTemplate 中使用自定义 ObjectMapper 应该通过转换器设置,类似

Using customized ObjectMapper within RestTemplate should be set through converters, something like

    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(objectMapper);
    messageConverters.add(jsonMessageConverter);
    restTemplate.setMessageConverters(messageConverters);
    return restTemplate;

这篇关于Java:使用“@ class”将其中的json反序列化为rest模板中的object在json -SpringBoot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆