如标题所示,我正在运行 Java RS Rest API。成功调用的 GET 方法返回具有正确数据的对象。但是,返回值后出现错误,导致浏览器中显示 500 页面。
Get 方法如下所示:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Verplaatsing> getAll(@QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
/* First check the auth code */
/* First check the auth code */
if (authCode == null || !authCode.equals(service.getAuthcode(rightAll))) {
throw new WebApplicationException("Invalid or no auth key provided");
}
List<Verplaatsing> verplaatsing = new ArrayList<>();
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date start = dateFormat.parse(sD);
Date end = dateFormat.parse(eD);
verplaatsing = service.getAllVerplaatsing(start, end);
} catch (Exception ex) {
throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
}
return verplaatsing;
}
REST 服务返回的 Verplaatsing 类如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "verplaatsing")
@Entity (name = "VERPLAATSING")
@IdClass(VerplaatsingId.class)
public class Verplaatsing implements Serializable
{
@ManyToOne @Id
private Registratiekast registratiekast;
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@Id
private Date verplaatsingDatum;
private double positie;
private double snelheid;
private String edge;
private String lane;
protected Verplaatsing()
{
}
public Verplaatsing(Registratiekast registratiekast, Date date, double positie, double snelheid, String edge, String lane)`enter code here`
{
this.registratiekast = registratiekast;
this.verplaatsingDatum = date;
this.positie = positie;
this.snelheid = snelheid;
this.edge = edge;
this.lane = lane;
}
public Date getVerplaatsingDatum()
{
return verplaatsingDatum;
}
public String getEdge()
{
return edge;
}
public String getLane()
{
return lane;
}
public double getPositie()
{
return positie;
}
public Registratiekast getRegistratiekast()
{
return registratiekast;
}
public double getSnelheid()
{
return snelheid;
}
@Override
public String toString() {
return "verplaatsing [registratiekast=" + this.registratiekast + ", date=" + this.verplaatsingDatum + ", positie="
+ this.positie + ", snelheid=" + this.snelheid + ", edge=" + this.edge + "]";
}
}
奇怪的是,在 GET 请求失败后没有日志项。这使得调试这个问题几乎不可能。我对代码所做的一个主要更改是将 Verplaatsing 类移动到另一个 .jar。但是,我无法想象这是导致此问题的原因。
编辑:
整个WebService代码:
@Path("verplaatsing")
@Stateless
public class VerplaastingREST {
private static String rightAll = "all";
private static String rightSingle = "single";
@Inject
VerplaatsingService service;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Verplaatsing> getAll(@QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
/* First check the auth code */
/* First check the auth code */
if (authCode == null || !authCode.equals(service.getAuthcode(rightAll))) {
throw new WebApplicationException("Invalid or no auth key provided");
}
List<Verplaatsing> verplaatsing = new ArrayList<>();
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date start = dateFormat.parse(sD);
Date end = dateFormat.parse(eD);
verplaatsing = service.getAllVerplaatsing(start, end);
} catch (Exception ex) {
throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
}
return verplaatsing;
}
@GET
@Path("{text}")
@Produces(MediaType.APPLICATION_JSON)
public List<Verplaatsing> getVerplaatsingen(@PathParam("text") String id, @QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
/* First check the auth code */
if (authCode == null || (!authCode.equals(service.getAuthcode(rightAll)) && !authCode.equals(service.getAuthcode(rightSingle)))) {
throw new WebApplicationException("Invalid or no auth key provided");
}
List<Verplaatsing> verplaatsing = new ArrayList<>();
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date start = dateFormat.parse(sD);
Date end = dateFormat.parse(eD);
verplaatsing = service.getAllVerplaatsing(id ,start, end);
} catch (ParseException ex) {
throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
}
return verplaatsing;
}
}
最佳答案
好的,我找到了解决方案。我手动将实体 .jar 中的所有实体添加到 Web 服务的 .class 列表中。在 ApplicationConfig.java 文件中:
package org.netbeans.rest.application.config;
import com.s63c.verplaatsingdata.Registratiekast;
import com.s63c.verplaatsingdata.Verplaatsing;
import java.util.Set;
import javax.ws.rs.core.Application;
/**
*
* @author Koenkk
*/
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
resources.add(Verplaatsing.class);
resources.add(Registratiekast.class);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.s63c.verplaatsingsysteem.rest.VerplaastingREST.class);
}
}
不知道为什么这会突然起作用,因为它应该开箱即用。但是嗯。
关于Java RS Rest API 方法成功返回值,状态码为 500,服务器日志中无异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22845412/