我正在尝试使用Java(使用Netbeans,Glassfish和Jersey)编写REST Web服务。

我写了一个小应用程序,它可以工作。但是后来我尝试了以下方法:

package service;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import converter.FlyedpConverter;
import entities.FlysCombination;

@Path("/getdates")
public class FlyedpResource {

   @GET
   @Path("{date}")
   @Produces({"application/json"})
   public Response get(@PathParam("date") String date) throws IOException {
      FlysCombination comb = new FlysCombination();
      FlysCombination x = FlysCombination.instance();
      comb = x.getcomb(date);

      System.out.println(comb.toString());

      if (comb == null) {
         return Response.status(Response.Status.NOT_FOUND).build();
      }
      return Response.ok(comb).build();
   }
}

我的类FlysCombination:
package entities;

import converter.FlyedpConverter;
import java.util.ArrayList;

public class FlysCombination {

   public FlysCombination() {}

   public static FlysCombination flycomb = null;

   public static FlysCombination instance() {
      if (flycomb == null) {
         flycomb = new FlysCombination();
      }
      return flycomb;
   }

   private String session;
   @SuppressWarnings("rawtypes")
   private ArrayList combinations = new ArrayList();

   public FlysCombination(String session,ArrayList combinations) {
      this.session=session;
      this.combinations=combinations;
   }

   public ArrayList getcombinations() {
      return combinations;
   }

   public void setcombinations(ArrayList goingthere) {
      this.combinations = goingthere;
   }

   public String getsession() {
      return session;
   }

   public void setsession(String session) {
      this.session = session;
   }


   public FlysCombination getcomb(String a) {
      FlyedpConverter fly = new FlyedpConverter (a);
      combinations.add(fly);
      session="sdjkbsqdjhf";
      FlysCombination flycomb = new FlysCombination (session,combinations);

      return(flycomb);
   }
}

FlyedpConverter:
package converter;

public class FlyedpConverter {
   private String keydate;

   public FlyedpConverter(String keydate) {
      this.keydate=keydate;
   }

   public String getkeydate() {
      return keydate;
   }

   public void setkeydate(String keydate) {
      this.keydate = keydate;
   }
}

我收到以下错误:
Status: 500 (Internal Server Error) javax.ws.rs.WebApplicationException
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:268)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:680)

我认为问题出在@XmlRootElement中,我不知道在哪里写...当我调试时,com变量包含结果,所以问题出在return Response.ok(comb).build();

有人能帮我吗?

最佳答案

您需要用FlysCombination注释@XmlRootElement类,以让Jaxb执行序列化

10-07 23:50