Serialization of Jersey and Jackson subclasses does not include additional attributes

In Jersey, when using Jackson to serialize JSON, additional attributes of the 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 am returning the next json

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

But i really want

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

Also, in my web.xml, I allowed POJOMappingFeature to solve this problem

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

Edit: Fixed Java code so that the settings are set correctly and Foo is not abstract

+3
source share
2 answers

, ; : JAXB (), JAXB , getter/setter. setter 'biz' , .

; , JAXB ( , ). , @JsonProperty 'getBiz' .

, JAXB, - , JAXB XML, JSON, JAXB ( Jersey) XML. JSON.

+3

POJOMappingFeature, JAXB:

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

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

All Articles