java – 从restful webservice返回对象列表时出现错误500

我试图从我的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从我的数据库中填充了对象,因此命名查询正在运行.但是当我走到下一行返回vakken时;它跳转到Method.java第498行返回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再次拥有一个测试列表等.?

最佳答案 终于在4天以上找到了解决方案!

有几个问题:

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

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

添加寄存器(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 =“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修正了这个问题.

点赞