我正在尝试用Java编写REST Web服务(使用Netbeans,Glassfish和Jersey)。
我写了一个小应用程序,它的工作原理。但后来我尝试了以下内容:
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();
< / p>
有人可以帮助我吗?
答案 0 :(得分:2)
您需要使用FlysCombination
注释@XmlRootElement
班级,让Jaxb执行序列化
答案 1 :(得分:1)
如果未正确定义返回类型,则会出现此错误。请参阅..您的Web服务的返回类型为Application / json。
如果您尝试返回XML,则无法解决问题。您可以将返回类型更改为application / xml并将@XMLRootElement添加到bean中,也可以在bean中为json添加相应的类型。
答案 2 :(得分:0)
为了它的价值:我遇到了类似的问题。
我的JAXB类是在Maven配置中使用xjc自动生成的,因此我无法手动添加@XmlRootElement(每次构建都会重新生成代码)。
我使用xml类型作为另一个xml类型的一部分作为根元素,因此JAXB不添加XmlRootElement注释(See this article)。
此外,我不想将xml complexType修改为元素(尽管这可能是最常用的解决方案)。在这种情况下,我必须处理两个Bookmark对象(一个来自数据模型,一个来自jaxb模型),这太混乱了。
所以我覆盖了默认的提供者,代码:
@Provider
@Produces(MediaType.APPLICATION_XML)
public class CustomBodyWriter extends AbstractRootElementProvider {
public CustomBodyWriter(@Context Providers ps) {
super(ps, MediaType.APPLICATION_XML_TYPE);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation annotations[], MediaType mediaType) {
return isSupported(mediaType);
}
@Override
protected void writeTo(Object t, MediaType mediaType, Charset c, Marshaller m, OutputStream entityStream) throws JAXBException {
if (t instanceof BookmarkType) {
JAXBElement<BookmarkType> bookmark = new ObjectFactory().createBookmark((BookmarkType) t);
m.marshal(bookmark, entityStream);
} else {
m.marshal(t, entityStream);
}
}
}
希望这可以帮助某人。