【问题标题】:Json mapping with JaxB annotation using Jackson使用 Jackson 进行 JaxB 注释的 Json 映射
【发布时间】:2014-04-08 06:36:37
【问题描述】:

如何使用 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 没有使用 @XmlElementWrapper @XmlSeeAlso 注释,Jackson 也没有映射 @XmlRootElement 注释。我正在使用杰克逊 1.9.0。 Jackson 将元素放入列表中,但不映射 POJO 类的根元素。

这里是示例方法。

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();
}
}

它生成的响应是 '{"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"}]}}'

不在 PBBeans 标记中获取 bean 的根名称。

【问题讨论】:

  • 如何调用序列化方法?请使用示例数据将您的问题扩展到该代码。
  • 其实我用的是 ApacheCXF,JAX-RS 和 spring。
  • 好的,但是请告诉我们您的网络服务方法,它是/不工作的。

标签: java json web-services jaxb jackson


【解决方案1】:

希望this 有所帮助。基本上,您需要在映射器中将 WRAP_ROOT_VALUE 设置为 true。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2011-12-20
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多