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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With