我正在尝试从宁静的Web服务返回对象列表,但我不断收到错误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)

任何帮助将不胜感激!

编辑

我想我找到了问题,但我不知道如何解决。

当我从 class 中删除任何相关对象时,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再次拥有一个测试列表,等等。

最佳答案

超过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");
    }
}

我的实体错了。我从吸气剂中删除了所有@XmlElement,并添加了@XmlAccessorType(XmlAccessType.FIELD)和@XmlInverseReference(mappedBy =“something”)

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修复了此问题。

10-04 18:55