Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 返回类型为marker接口时使用Jackson的多态序列化_Rest_Spring Mvc_Jackson_Jsonserializer - Fatal编程技术网

Rest 返回类型为marker接口时使用Jackson的多态序列化

Rest 返回类型为marker接口时使用Jackson的多态序列化,rest,spring-mvc,jackson,jsonserializer,Rest,Spring Mvc,Jackson,Jsonserializer,我有一个rest服务,它返回marker接口,这个接口有多个实现,并且在实现中没有任何公共属性 @RequestMapping(value = "/users/{userName}", method = RequestMethod.GET) public User getUser(@PathVariable("userName") String userName) { return userService.getUser(userName); } 用户及

我有一个rest服务,它返回marker接口,这个接口有多个实现,并且在实现中没有任何公共属性

    @RequestMapping(value = "/users/{userName}", method = RequestMethod.GET)
    public User getUser(@PathVariable("userName") String userName) {
        return userService.getUser(userName);
    }
用户及其实现。注意:用户是标记接口

public interface User {}

public AdminUser implements User { // some properties & its setters & getters } 

public SupportUser implements User { // some properties & its setters & getters } 
我使用Jackson 1.9.1来序列化和反序列化

当我点击上面的服务时,我得到的是下面的回应

{}

当我调试它时,我看到用户实现对象已准备好并发送回Jackson进行序列化。但Jackson正在向浏览器发送空响应。当返回类型为marker interface时,有人可以建议如何使用serialize。

您可以使用
@JsonTypeInfo
注释,该注释添加了一个属性以在浏览器之间共享类型信息序列化/反序列化。
public interface User {}

public AdminUser implements User { // some properties & its setters & getters } 

public SupportUser implements User { // some properties & its setters & getters } 

此处阅读更多信息:

使用
@JsonTypeInfo
@JsonSubTypes
来反序列化多态类型,这些多态类型在序列化java对象和重新创建确切的子类型时维护子类型信息

举个例子,动物是一个界面,它可以是老虎或狮子,它们都扩展了动物界面。在反序列化过程中,我们希望创建精确的动物类型,并演示@JsonTypeInfo和@JsonSubTypes注释的使用

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
        include=JsonTypeInfo.As.PROPERTY,
        property="name")
@JsonSubTypes({
        @JsonSubTypes.Type(value=Lion.class, name="lion"),
        @JsonSubTypes.Type(value=Tiger.class, name="tiger"),
})
public interface Animal {

}

@JsonTypeName("lion")
public class Lion implements Animal {

    private String name;
    private String roar;

//constructor & setters & getters
}

@JsonTypeName("tiger")
public class Tiger implements Animal {

    private String name;
    private String purr;

//constructor & setters & getters
}
主要方法:

List<Animal> animal = new ArrayList<Animal>();
animal.add(new Lion("lion", "roar"));
animal.add(new Tiger("tiger", "purr"));
animal.add(new Tiger("tiger", "purrrrrrrrr"));

URL url = JacksonPolymorphicSerialization.class.getClassLoader().getResource("animals.json");
File file = new File(url.getPath());

// de-serailization sub types
new ObjectMapper().writeValue(file, animal);

// serailization animals and its subtype
List<Animal> animals = new ObjectMapper().readValue(file, List.class);
System.out.println(animals);


output : [{name=lion, roar=roar}, {name=tiger, purr=purr}, {name=tiger, purr=purrrrrrrr}]
List animal=new ArrayList();
添加(新狮子(“狮子”、“咆哮”));
添加(新老虎(“老虎”、“呜呜”));
动物。添加(新老虎(“老虎”、“呼噜”));
URL URL=JacksonPolymorphicsSerialization.class.getClassLoader().getResource(“anies.json”);
File File=新文件(url.getPath());
//去血清化子类型
新建ObjectMapper().writeValue(文件,动物);
//血清化动物及其亚型
List animals=new ObjectMapper().readValue(文件,List.class);
系统输出打印(动物);
输出:[{name=lion,roar=roar},{name=tiger,purr=purr},{name=tiger,purr=purrr}]

希望这有助于您理解使用Jackson序列化和反序列化多态类型。

没有其他人关心将基类耦合到其子类吗?@KartikChugh是的,我同意您的看法。。。创造这样的政变真的很糟糕。我发现这是一个最干净的方法@KartikChugh我想说在这种情况下,正常的OOP设计是有道理的。@redigaffi违反了SRP和依赖项反转: