1

How to serialize this class using Jackson

package com.progressivebeef.service.response;

@XmlRootElement(name = "response")
@XmlSeeAlso({ User.class,             Profile.class,MenuItem.class,Feedlot.class,Document.class,FeedlotDocument.class })
public final class PBResponse {

private Integer status = FAILURE;
private String code;
private String message;
private Integer totalRecords;
private List<Model> list = new ArrayList<Model>();


public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

@XmlElementWrapper(name = "PBBeans")
@XmlAnyElement
public List<Model> getList() {
    return list;
}

public void setList(List<Model> list) {
    this.list = list;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Integer getTotalRecords() {
    return totalRecords;
}

public void setTotalRecords(Integer totalRecords) {
    this.totalRecords = totalRecords;
}

/**
 * @author tiqbal
 * Resets the response.
 */
public void reset(){

    this.status = FAILURE;
    this.list = new ArrayList<Model>();
    this.code = null;
    this.message = null;
    this.totalRecords = null;
}
}

Jackson is not picking up @XmlElementWrapper @XmlSeeAlso annotations, also Jackson is not mapping @XmlRootElement annotation. I am using Jackson 1.9.0. Jackson is putting elements in the list but not mapping root element of POJO classes.

Here is sample method.

package com.progressivebeef.service.impl;

@Service("/ActivityServiceImpl/")
@Path("/activityservice/")
public class ActivityServiceImpl implements ActivityService {

@POST
@Produces(MediaType.APPLICATION_JSON)
@Override
public Response inputJson(User user ) {

    System.out.println("user ");


    user.setUser_name("check user name");

    Profile profile = new Profile();

    profile.setFirst_name("abc");
    profile.setLast_name("khan");

    user.setProfile( profile );

    PBResponse response = new PBResponse();
    response.getList().add(user);

    return Response.ok(response).build();
}
}

The response it generating is '{"response":{"status":0,"PBBeans":[{"user_name":"check user name","password":"click123","user_role_key":2,"profile":{"first_name":"abc","last_name":"khan","tableName":"pb_profile","pk":"profile_id"},"tableName":"pb_user","pk":"user_id"}]}}'

not picking up the bean's root name inside PBBeans tag.

Taha Iqbal
  • 21
  • 4

1 Answers1

0

Hope this helps. Basically, you need to set the WRAP_ROOT_VALUE to true in your mapper.

Community
  • 1
  • 1
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49