从restful webservice返回对象列表时出错500

时间:2016-10-29 18:23:18

标签: java web-services rest

我试图从我的restful webservice返回一个对象列表但是我一直收到错误500,内部服务器错误。

服务器遇到内部错误,导致无法完成此请求。

当我手动创建对象时,api就像预期的那样工作。

@GET
@Path("getAll")
@Produces(MediaType.APPLICATION_XML)
public List<Vak> getAllVakken() {
    List<Vak> vakken = new ArrayList<>();
    Vak v = new Vak("vak1");
    Vak v2 = new Vak("vak2");
    vakken.add(v);
    vakken.add(v2);

    return vakken;
}

当我将功能更改为以下功能时,它不再起作用。

@GET
@Path("getAll")
@Produces(MediaType.APPLICATION_XML)
public List<Vak> getAllVakken() {
    List<Vak> vakken = em.createNamedQuery("Vak.getAll").getResultList();
    return vakken;
}

调试函数时,List<Vak> vakken会填充数据库中的对象,因此命名查询正在运行。但当我走到下一行return vakken;时,它跳转到Method.java第498行return ma.invoke(obj, args);

Vak.java

@NamedQueries({
    @NamedQuery(name = "Vak.getAll", query = "select v from Vak v")
})

@Entity
@XmlRootElement
public class Vak implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String naam;

    @OneToMany(mappedBy = "vak")
    private List<Test> testen = new ArrayList<>();


    public Vak() {
    }

    public Vak(String naam) {
        this.naam = naam;
    }

    @XmlElement
    public Long getId() {
        return id;
    }

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

    @XmlElement
    public String getNaam() {
        return naam;
    }

    public void setNaam(String naam) {
        this.naam = naam;
    }

    @XmlElement
    public List<Test> getTesten() {
        return testen;
    }

    public void setTesten(List<Test> testen) {
        this.testen = testen;
    }


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Vak)) {
            return false;
        }
        Vak other = (Vak) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "info.toegepaste.www.entity.Vak[ id=" + id + " ]";
    }

}

Test.java

@NamedQueries({
    @NamedQuery(name = "Test.getAll", query = "select t from Test t order by t.vak")
})

@Entity
@XmlRootElement
public class Test implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private float behaaldCijfer, maxScore;
    private String naam;
    @Temporal(TemporalType.DATE)
    private GregorianCalendar datum;

    @ManyToOne
    private Vak vak;

    @ManyToOne
    private Student student;

    public Test() {
    }

    public Test(String naam, float behaaldCijfer, float maxScore, GregorianCalendar datum, Student student, Vak vak) {
        this.maxScore = maxScore;
        this.behaaldCijfer = behaaldCijfer;
        this.naam = naam;
        this.datum = datum;
        this.vak = vak;
        this.student = student;
    }

    @XmlElement
    public Long getId() {
        return id;
    }

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

    @XmlElement
    public float getBehaaldCijfer() {
        return behaaldCijfer;
    }

    public void setBehaaldCijfer(float behaaldCijfer) {
        this.behaaldCijfer = behaaldCijfer;
    }

    @XmlElement
    public float getMaxScore() {
        return maxScore;
    }

    public void setMaxScore(float maxScore) {
        this.maxScore = maxScore;
    }

    @XmlElement
    public String getNaam() {
        return naam;
    }

    public void setNaam(String naam) {
        this.naam = naam;
    }

    @XmlElement
    public GregorianCalendar getDatum() {
        return datum;
    }

    public void setDatum(GregorianCalendar datum) {
        this.datum = datum;
    }

    @XmlElement
    public Vak getVak() {
        return vak;
    }

    public void setVak(Vak vak) {
        this.vak = vak;
    }

    @XmlElement
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
}

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Test)) {
            return false;
        }
        Test other = (Test) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return naam;
    }

}

调用该函数时,我的服务器客户端的输出窗口中没有任何错误。但是当我从客户端调用该函数时,我得到了这个:

来自客户端的

GlassFish输出

Severe:   javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:877)
at org.glassfish.jersey.client.JerseyInvocation.access$800(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:722)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:718)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:430)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:321)
at info.toegepaste.www.ManageServlet.getApiVak(ManageServlet.java:148)
at info.toegepaste.www.ManageServlet.processRequest(ManageServlet.java:126)
at info.toegepaste.www.ManageServlet.doPost(ManageServlet.java:179)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Warning:   StandardWrapperValve[ManageServlet]: Servlet.service() for servlet ManageServlet threw exception
java.lang.NullPointerException
at info.toegepaste.www.ManageServlet.processRequest(ManageServlet.java:142)
at info.toegepaste.www.ManageServlet.doPost(ManageServlet.java:179)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

非常感谢任何帮助!

修改

我认为我发现了问题,但我不知道如何解决它。

当我从我的类中删除任何相关对象时,API可以正常工作。 所以我的班级Vak包含一系列测试。如果我手动创建一个新列表,我将Vak的对象放入其中,而不引用Test它就可以了。

@GET
@Path("getAll")
@Produces(MediaType.APPLICATION_XML)
public List<Vak> getAllVakken() {

    List<Vak> vakken1 = em.createNamedQuery("Vak.getAll").getResultList();
    List<Vak> vakken2 = new ArrayList<>();

    for (Vak vak : vakken1) {
        Vak v = new Vak(vak.getNaam());
        vakken2.add(v);
    }

    return vakken2;
}

上面的代码按照我想要的方式以适当的XML布局返回Vak的所有对象。

当我添加行v.setTesten(vak.getTesten());时,我收到错误500。

@GET
@Path("getAll")
@Produces(MediaType.APPLICATION_XML)
public List<Vak> getAllVakken() {

    List<Vak> vakken1 = em.createNamedQuery("Vak.getAll").getResultList();
    List<Vak> vakken2 = new ArrayList<>();

    for (Vak vak : vakken1) {
        Vak v = new Vak(vak.getNaam());
        v.setTesten(vak.getTesten());
        vakken2.add(vak);
    }

return vakken2;
}

所以问题必须放在这里。它是否存在无限循环问题? Vak 包含一个测试列表,其中每个 Test 包含 Vak 的对象,其中每个 Vak 再次拥有一个列表测试等..?

1 个答案:

答案 0 :(得分:1)

超过4天后终于找到了解决方案!

有几个问题:

将以下依赖项添加到您的pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.19</version>
</dependency>

在根应用程序中添加register(MoxyXmlFeature.class);

@ApplicationPath("rest")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        super(MultiPartFeature.class);
        register(MoxyXmlFeature.class);
        packages("info.toegepaste.www.ejb"); 
        packages("info.toegepaste.www.entity");
    }
}

我的实体错了。我从getter中删除了所有@XmlElement并添加了@XmlAccessorType(XmlAccessType.FIELD)和@XmlInverseReference(mappedBy =&#34;某些东西&#34;)

<强> Vak.java

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Vak implements Serializable {

private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String naam;

  @OneToMany(mappedBy = "vak", cascade = CascadeType.ALL)
  @XmlElement
  @XmlInverseReference(mappedBy = "vak")
  private List<Test> testen = new ArrayList<>();

  public Vak() {
  }

  public Vak(String naam) {
    this.naam = naam;
  }

  //no @XmlElement here!
  public String getNaam() {
    return naam;
  }

  .....
}

<强> Test.java

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Test implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private float behaaldCijfer, maxScore;
  private String naam;
  @Temporal(TemporalType.DATE)
  private GregorianCalendar datum;

  @ManyToOne
  @XmlElement
  private Vak vak;

  @ManyToOne
  @XmlElement
  private Student student;

  public Test() {
  }

  .....
}

修复代码后,我还遇到了GlassFish的错误。 Have a look here.我使用Payara修复此问题。