8

我正在使用 GlassFish 4.0 服务器和基于 JPA 的服务器端类,我想通过 JAX-RS 提供这些类。到目前为止,这对于简单实体来说效果很好。但是,例如,如果我有一个 @OneToMany 关系并且有一个链接实体,则服务器会返回 500 内部服务器错误。在这种情况下,服务器日志中不会记录任何内容。为了找到错误,我创建了一个小的自定义 JSP 页面来获取有关所发生情况的更多信息。代码就是这样:

Status: <%= pageContext.getErrorData().getStatusCode() %>
Throwable: <%= pageContext.getErrorData().getThrowable() %>

不幸的是,输出只是“状态:500 Throwable:null”

我自己的服务器端代码似乎运行正常(做了一些调试输出),但是出现了一些错误。在此示例中,除非存在链接的 IssueComment 实体,否则可以毫无问题地检索 User 和 Issue 类:

用户等级:

package my.application.model;

import static javax.persistence.FetchType.LAZY;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * The persistent class for the User database table.
 * 
 */
@XmlRootElement
@Entity(name="User")
@Table(name="User")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="failedLogin")
    private short failedLogin;

    @Column(name="firstname")
    private String firstname;

    @Column(name="lastname")
    private String lastname;

    @Column(name="middlename")
    private String middlename;

    @Column(name="password")
    private String password;

    @Column(name="username")
    private String username;

    //bi-directional many-to-one association to IssueComment
    @OneToMany(mappedBy="user", fetch = LAZY)
    private List<IssueComment> issueComments;

    //bi-directional many-to-one association to SignalComment
    @OneToMany(mappedBy="user", fetch = LAZY)
    private List<SignalComment> signalComments;

    //bi-directional many-to-one association to SignalMeasure
    @OneToMany(mappedBy="user", fetch = LAZY)
    private List<SignalMeasure> signalMeasures;

    public User() {
    }

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

         // more getters and setters auto-generated by Eclipse
        }

用户等级:

package my.application.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

@NamedQuery(
    name = "getSingleIssue",
    query = "SELECT i FROM Issue i WHERE i.id = :id"
)
/**
 * The persistent class for the Issue database table.
 * 
 */
@XmlRootElement
@Entity(name="Issue")
@Table(name="Issue")
public class Issue implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="concernedModule")
    private String concernedModule;

    @Column(name="createdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdate;

    @Column(name="duedate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date duedate;

    @Column(name="priority")
    private int priority;

    @Column(name="reminderdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date reminderdate;

    @Column(name="responsibleUserId")
    private int responsibleUserId;

    @Column(name="sendingModule")
    private String sendingModule;

    @Column(name="severity")
    private int severity;

    @Column(name="status")
    private int status;

    @Column(name="title")
    private String title;

    // bidirectional many-to-one association to IssueComment
    @OneToMany(mappedBy = "issue")
    private List<IssueComment> issueComments;

    public Issue() {
    }

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

 // more getters and setters....
}

问题评论:

package my.application.model;

import java.io.Serializable;

import javax.persistence.*;

import java.util.Date;


/**
 * The persistent class for the IssueComment database table.
 * 
 */
@Entity(name="IssueComment")
@Table(name="IssueComment")
public class IssueComment implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    @Lob
    @Column(name="comment")
    private String comment;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="time")
    private Date time;

    //bi-directional many-to-one association to Issue
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="issueId")
    private Issue issue;

    //bi-directional many-to-one association to User
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="userId")
    private User user;

    public IssueComment() {
    }

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

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

 // getters/setters....
}

网络服务如下:

package my.application.server.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.glassfish.jersey.server.ResourceConfig;

import my.application.data.UserStorage;
import my.application.logger.Logger;
import my.application.model.Signal;
import my.application.model.SignalComment;
import my.application.model.User;

@Provider
@Path("User")
public class UserService extends ResourceConfig {

    private UserStorage storage = new UserStorage();

    public UserService() {
        this.packages("my.application.model");
    }

    @Produces(MediaType.APPLICATION_XML)
    @Path("load")
    @GET
    public User getUser(@QueryParam("id") int id) {
        try {
            Logger.getInstance().log("fetching id: " + id);
            User u = storage.getUser(id);
            Logger.getInstance().log("number of signal comments: " + u.getSignalComments().size());
            SignalComment sc = u.getSignalComments().get(0);
            Logger.getInstance().log("Signal 0 comment: " + sc.getComment());
            Signal s = sc.getSignal();
            Logger.getInstance().log("Signal subject: " + s.getSubject());
            return u;

        } catch (Exception e) {
            e.printStackTrace();
        }
        // this code is not being reached (so no errors in this method):
        Logger.getInstance().log("---EXCEPTION HAS BEEN THROWN---");

        return null;
    }
}

我放弃了客户端源代码,因为它是服务器端的,可以用普通浏览器复制,所以恕我直言,这里不需要客户端代码。

4

1 回答 1

8

确保您尝试编组为 XML 的图形(对象)中没有任何循环引用。例如,这可能会导致问题:

User -> IssueComment -> (the same) User

或者

User -> IssueComment -> Issue -> IssueComment -> (the same) User

此类结构不能编组为 XML。

注意:添加@XmlRootElement注释IssueComment(我认为不需要,但最好有它)。

注意:我们知道日志记录问题,它将作为JERSEY-2000的一部分解决。

于 2013-08-15T14:09:09.123 回答