简体   繁体   English

Java:使用json -SpringBoot中的“​​ @class”将json序列化为其余模板中的对象

[英]Java: Deserialize a json to object in rest template using “@class” in json -SpringBoot

I have to instantiate a class which extends the abstract class from JSON using information in @class as shown below. 我必须实例化一个类,该类使用@class中的信息从JSON扩展了抽象类,如下所示。

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

Here the abstract class is animal and dog extends the animal class. 这里的抽象类是动物,而狗则扩展了动物类。 So using the information in @class, can we instantiate dog directly. 因此,使用@class中的信息,我们可以直接实例化dog吗。 Also this is the response I am getting in restTemplate 这也是我在restTemplate中得到的响应

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

The following error is coming when this line is executed. 执行此行时,将出现以下错误。 Since the POJO classes are auto-generated, I cannot use annotations like @JsonTypeInfo 由于POJO类是自动生成的,因此无法使用@JsonTypeInfo之类的注释

I am using Spring boot and maven. 我正在使用Spring Boot和Maven。 This error is coming in console. 控制台中出现此错误。

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 无法读取JSON:无法构造“ MyPOJO”的实例,问题:抽象类型需要映射到具体类型,具有自定义反序列化器或使用其他类型信息进行实例化

You can use @JsonTypeInfo annotation regardless of the fact that the classes are generated, by adhering to MixIn's 您可以使用@JsonTypeInfo批注,而无需考虑生成类的事实,只要遵守MixIn的

"mix-in annotations are": a way to associate annotations with classes, without modifying (target) classes themselves. “混合注释是”:一种将注释与类相关联的方法,而无需修改(目标)类本身。

That is, you can: 也就是说,您可以:

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) 定义将混合类(或接口)的注释与目标类(或接口)一起使用,以使其看起来像目标类具有混合类具有的所有注释(出于配置序列化/反序列化)

So you can write your AnimalMixIn class, something like 因此,您可以编写AnimalMixIn类,例如

@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  
{  

}  

and configure your deserializer 并配置您的解串器

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

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 由于您使用的春天启动,您可以查看以下博客文章,看你如何自定义ObjectMapper使用混入, 自杰克逊对象映射器 ,尤其要注意混入的方法Jackson2ObjectMapperBuilder

Using customized ObjectMapper within RestTemplate should be set through converters, something like RestTemplate使用自定义的ObjectMapper应该通过转换器进行设置,例如

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

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

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