本文介绍了GlassFish 4.0 w / Jersey返回500个内部服务器错误,无一例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 状态:<%= pageContext.getErrorData()。getStatusCode()%> 
Throwable:<%= pageContext.getErrorData()。getThrowable()%>

不幸的是,输出只是Status:500 Throwable:null

我自己的服务器端代码似乎可以正常运行(执行了一些调试输出),但是会出现一些错误。在这个例子中,User和Issue类可以在没有问题的情况下被检索,除非有一个链接的IssueComment实体:

用户类:

  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;

/ **
*用户数据库表的持久类。
*
* /
@XmlRootElement
@Entity(name =User)
@Table(name =User)
public class User实现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;

//双向多对一关联IssueComment
@OneToMany(mappedBy =user,fetch = LAZY)
private List< IssueComment> issueComments;

//双向多对一关联到SignalComment
@OneToMany(mappedBy =user,fetch = LAZY)
private List< SignalComment> signalComments;

//双向多对一关联到SignalMeasure
@OneToMany(mappedBy =user,fetch = LAZY)
private List< SignalMeasure> signalMeasures;

public User(){
}

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

// Eclipse自动生成的更多getter和setter
}

User class:

  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发行i WHERE i.id =:id

/ **
*问题数据库表的持久类。
*
* /
@XmlRootElement
@Entity(name =Issue)
@Table(name =Issue)
public class Issue实现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创建日期;

@Column(name =duedate)
@Temporal(TemporalType.TIMESTAMP)
private 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严重性;

@Column(name =status)
private int status;

@Column(name =title)
私人字符串标题;

//双向多对一关联到IssueComment
@OneToMany(mappedBy =issue)
private List< IssueComment> issueComments;

public Issue(){
}

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

//更多getters和setters ...
}

IssueComment:

  package my.application.model; 

import java.io.Serializable;

import javax.persistence。*;

import java.util.Date;


/ **
* IssueComment数据库表的持久类。
*
* /
@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)
私人字符串评论;

@Temporal(TemporalType.TIMESTAMP)
@Column(name =time)
私人日期时间;

//双向多对一关联来发行
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =issueId)
私人问题;

//与用户
的双向多对一关联@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =userId)
私人用户用户;

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;

导入org.glassfish.jersey.server.ResourceConfig;

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

@Provider
@Path(User)
public class UserService extends ResourceConfig {
$ b $ private UserStorage storage = new UserStorage();

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

$ b @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(信号注释数量:+ 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());
返回你;

} catch(Exception e){
e.printStackTrace();
}
//这段代码没有被到达(所以在这个方法中没有错误):
Logger.getInstance().log(--- EXCEPTION HAS BEEN THROWN --- );

返回null;




$ b我离开了客户端源代码,并且可以使用普通的浏览器进行复制,所以没有必要在这里为客户端代码。恕我直言。 解决方案

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

 用户 - > IssueComment  - > (相同)User 

 用户 - > IssueComment  - >问题 - > IssueComment  - > (相同)User 

这样的结构不能编入XML。



注意: @XmlRootElement 注解 IssueComment (I认为它不是必需的,但它最好在那里)。



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


I'm using a GlassFish 4.0 server and server-sided JPA-based classes, which I want to deliver via JAX-RS. This works fine so far for simple entities. However, if I have a @OneToMany relation for example AND there is a linked entity, the server returns a 500 internal server error. In that case, nothing is logged to the server log. In order to find the error, I created a small custom JSP page to get more info about what happened. The code is just this:

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

Unfortunately, the output is just "Status: 500 Throwable: null"

My own server-sided code seems to run properly (did some debug output), but however, some error emerges. In this example, the User and Issue classes can be retrieved without a problem unless there is a linked IssueComment entity:

User class:

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
        }

User class:

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

The Webservice is as follows:

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

I left away the client source code since it's server-sided and can be reproduced with a normal browser, so no necessity for client code here IMHO.

解决方案

Make sure you don't have any cyclic references in graph (objects) you're trying to marshall to XML. For example, this could cause a problem:

User -> IssueComment -> (the same) User

or

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

Such structures cannot be marshalled into XML.

Note: Add @XmlRootElement annotation to IssueComment (I think it's not needed but it's better to have it there).

Note: We know about the logging issue and it will be solved as a part of JERSEY-2000.

这篇关于GlassFish 4.0 w / Jersey返回500个内部服务器错误,无一例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:02