【问题标题】:Alternative to Jackson @JsonSubTypes替代杰克逊@JsonSubTypes
【发布时间】:2014-08-29 04:40:15
【问题描述】:

Jackson 框架提供了基于注解的方法来在序列化过程中发出类型信息。

我不想在我的超类(Animal)中使用@JsonSubTypes 注释。

相反,我想告诉我的子类,即 Dog 和 Elephant Animal 是它们的父类。

有没有什么方法可以在 Animal 类中不使用注解。

如果是,请尽可能提供示例。

以下是我正在尝试解决的情况。“test”接收到的 JSON 包含“type”字段为“dog”或“elephant”。

我想将这两个类注册为“Animal”类的子类型,但不想在 Animal 中使用 @JsonSubTypes。

任何帮助将不胜感激。 提前致谢。

@JsonTypeInfo( use = JsonTypeInfo.Id.NAME,  include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class Animal(){
      private String sound;
      private String type;

     //getters and setters

}

@JsonTypeName("dog")
Class Dog extends Animal(){
     //some attributes.
     //getters and setters
}

@JsonTypeName("elephant")
Class Elephant extends Animal(){
     //some attributes.
     //getters and setters
}


@Controller
public class MyController {

    //REST service
    @RequestMapping( value = "test")
    public  @ResponseBody String save(@RequestBody  Animal animal){

    System.out.println(animal.getClass());
    return success;

    }
}

【问题讨论】:

  • 您可以通过调用 objectMapper.registerSubtypes(Dog.class, Elephant.class) 方法注册您的子类型。是你要找的吗?
  • 谢谢Alexey。请你举个例子来支持你的回答。
  • 我尝试了 objectMapper.registerSubtypes(Dog.class, Elephant.class) 但它没有用。
  • 我包含了 ObjectMapper mapper = new ObjectMapper(); mapper.registerSubtypes(Dog.class,Elephant.class);在 MyController 类的“保存”方法(REST 服务)中。

标签: java json spring-mvc jackson


【解决方案1】:

此答案将有助于实现您想要的,但方式略有不同。 创建一个具有必要配置的单独类,并将其注册为 Animal 类的序列化/反序列化配置类,如下所示:

配置类:

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @Type(value = Elephant.class, name = "cat"),
    @Type(value = Dog.class, name = "dog") })
abstract class PolymorphicAnimalMixIn {
    //Empty Class
}

序列化或反序列化:

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

//Sample class with collections of Animal
class Zoo {  
  public Collection<Animal> animals;  
}

//To deserialize
Animal animal = mapper.readValue("string_payload", Zoo.class);

//To serialize
Animal animal = mapper.writeValueAsString(zoo);

参考来源:example 5

【讨论】:

  • 不再有效:/
  • @facundofarias 现在什么有效? mapper.addMixIn() 不起作用。
【解决方案2】:

您可以使用Moonwlker 库。

有了它,你可以像这样创建一个 ObjectMapper:

ObjectMapper objectMapper = new ObjectMapper();

 MoonwlkerModule module =
   MoonwlkerModule.builder()
     .fromProperty("type").toSubclassesOf(Animal.class)
     .build();

 objectMapper.registerModule(module);

然后使用该映射器进行(反)序列化。 Moonwlker 网站包含更多详细信息和配置选项。

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2016-04-30
    • 2011-08-25
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多