Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Jersey Jackson Serialize Subclass

Mojoy:

Hi I am creating a restful service using Jersey. To return the result I need to convert to json format. I have a Parent and child Class where only parent class values are sent but child class is not getting serialized.

Parent Class -


@JsonTypeInfo(use = Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Child.class)})
public abstract class Parent {
private String test = "hello";

public Parent () {
}
public Parent (String test) {
this.test = test;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}

Child Class -


public class Child extends Parent{
private String name;

public Contact() {
super();
}
public Contact(String name, String test) {
super(test);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Actual Class -


public class RestResponse {
private List result;
// default and one argument constructor...

public List getResult() {
return result;
}
public void setResult(List result) {
this.result= result;
}
}

Rest Controller -


@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/testJson")
public class MyRestController {



@GET
@Path("/search")
public RestResponse getContacts(@Context HttpHeaders headers) throws URISyntaxException {
Parent p = new Child("myName", "testString");
List parentList = new ArrayList();
parentList.add(p);
RestResponse resultResponse = new RestResponse();
resultResponse .setData(parentList );
return resultResponse;
}

But below is the result I get,


{
"result": [
{
"test": "testString"
}
]
}

Where the subclass's property name is not serialized. I tried the solution given in Jersey and Jackson serialization of subclasses does not include extra attributes, but still I see subclass properties are not getting converted.

Any help will be really appreciated. Thanks in advance.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Jersey Jackson Serialize Subclass

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×