2

已编辑:我正在尝试编组使用 Jersey 提供的自定义对象(我的持久层由 EclipseLink 提供)。我所有的代码都运行良好,但是当我调用服务时我遇到了一个问题,因为它返回一个没有键的 JSON 对象,只有值。

要返回 JSON,我只需使用 Jersey 提供的标准类,并在模型中包含 XML 注释(用于 JAXB)。

有谁知道我怎样才能打印出对象属性名称(键)?

提前致谢!

泽西岛资源

(...)
@Path("/User")
public class User {
    @GET 
    @Path("/search")
    @Produces(MediaType.APPLICATION_JSON)
     public List<model.User> getUserByNick(@QueryParam("nick") String nick) {
         UserDao u = new UserDao();
         List<model.User> users = u.getUserByNick2(nick);

         return users;
     }
}

Json 结果:

[["test1","active"]]

我的模型对象是:

package model;

import java.io.Serializable;
import javax.persistence.*;

import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement(name = "user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;

    @XmlElement
    private byte acceptedconditions;

    @XmlElement
    private String conditionshash;

    @XmlElement
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationdate;

    @XmlElement
    private String email;

    @XmlElement
    private String nick;

    @XmlElement
    private String password;

    //bi-directional many-to-one association to Request
    @XmlElement
    @OneToMany(mappedBy="user")
    private List<Request> requests;

    //bi-directional many-to-one association to Language
    @XmlElement
    @ManyToOne
    @JoinColumn(name="languageid")
    private Language language;

    //bi-directional many-to-one association to Status
    @XmlElement
    @ManyToOne
    @JoinColumn(name="statusid")
    private Status status;

    //bi-directional many-to-one association to Usercompanydata
    @XmlElement
    @OneToMany(mappedBy="user")
    private List<Usercompanydata> usercompanydata;

    public User() {
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public byte getAcceptedconditions() {
        return this.acceptedconditions;
    }

    public void setAcceptedconditions(byte acceptedconditions) {
        this.acceptedconditions = acceptedconditions;
    }

    public String getConditionshash() {
        return this.conditionshash;
    }

    public void setConditionshash(String conditionshash) {
        this.conditionshash = conditionshash;
    }

    public Date getCreationdate() {
        return this.creationdate;
    }

    public void setCreationdate(Date creationdate) {
        this.creationdate = creationdate;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNick() {
        return this.nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Request> getRequests() {
        return this.requests;
    }

    public void setRequests(List<Request> requests) {
        this.requests = requests;
    }

    public Language getLanguage() {
        return this.language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    public Status getStatus() {
        return this.status;
    }

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

    public List<Usercompanydata> getUsercompanydata() {
        return this.usercompanydata;
    }

    public void setUsercompanydata(List<Usercompanydata> usercompanydata) {
        this.usercompanydata = usercompanydata;
    }

}
4

0 回答 0