Jersey and Jackson serialization of subclasses does not include extra attributes

白昼怎懂夜的黑 提交于 2019-12-12 15:11:20

问题


In Jersey, when using Jackson for JSON serialization, the extra attributes of an implementing subclass are not included. For example, given the following class structure

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="@class")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Foo.class, name = "foo")
}
public abstract class FooBase {
    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar( String bar ) {
        this.bar = bar;
    }
}

public class Foo extends FooBase {
    private String biz;

    public String getBiz() {
        return biz;
    }

    public void setBiz( String biz ) {
        this.biz = biz;
    }
}

And the following Jersey code

@GET
public FooBase get() {
   return new Foo();
}

I get back the following json

{"@class" => "foo", "bar" => null}

But what I actually want is

{"@class" => "foo", "bar" => null, "biz" => null}

Also, in my web.xml I have enabled POJOMappingFeature to solve this issue

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

Edit: Fixed the Java code to have the setters set properly and Foo to not be abstract


回答1:


It should work as you show; with one possible exception: if you enable JAXB annotations (only), JAXB restrictions mandate that only getter/setter pairs are used to detect properties. So try adding setter for 'biz' and see if that changes it.

This would not occur with Jackson annotations; and ideally not if you combine Jackson and JAXB annotations (I thought Jersey enabled both). If Jackson annotation processing is also enabled, adding @JsonProperty next to 'getBiz' should also do the trick.

Finally unless you need JAXB annotations, you could just revert to using Jackson annotations only -- in my opinion, the main use case for JAXB annotations is if you need to produce both XML and JSON, and use JAXB (via Jersey) for XML. Otherwise they aren't useful with JSON.




回答2:


Using POJOMappingFeature you can also annotate your classes with JAXB:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class FooBase {
    private String bar;
}

@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo extends FooBase {
    private String biz;
}


来源:https://stackoverflow.com/questions/6374421/jersey-and-jackson-serialization-of-subclasses-does-not-include-extra-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!