繁体   English   中英

GlassFish 4.0 w / Jersey毫无例外地返回500内部服务器错误

[英]GlassFish 4.0 w/ Jersey returns 500 internal server error without exception

我正在使用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....
}

IssueComment:

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....
}

Web服务如下:

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

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

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

User -> IssueComment -> (the same) User

要么

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

这样的结构不能编组成XML。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM