我的网络应用程序中有针对泽西的以下依赖项
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
当我使用@Consumes(MediaType.APPLICATION_JSON)
或@Produces(MediaType.APPLICATION_JSON)
时,我收到服务器错误。同样,如果我尝试使用XML。是因为我使用的库吗?
我的java类
import org.apache.log4j.Logger;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/test")
public class test {
private static Logger logger = Logger.getLogger(test.class);
@GET
@Produces(MediaType.TEXT_PLAIN) // works perfectly
public Response getMsg()
{
logger.info("Inside getMsg()");
String output = "hello world";
return Response.status(200).entity(output).build();
}
/* @GET // I get a HTTP 500 , server error
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
return track;
}
*/
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Response getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
//String test = " hello world";
return Response.status(201).entity(track).build();
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
// @Produces(Me)
public Response createTrackInJSON(Track track) {
String result = "Track saved : " + track;
return Response.status(201).entity(result).build();
}
@POST
@Path("/getValue")
@Produces(MediaType.APPLICATION_XML)
public void createXML()
{
String result = " get the track";
// return result.createXML();
// check this line
}
}
当我使用rest客户端访问@GET资源时,我收到此错误:
<u>Internal Server Error</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
当我使用其余客户端访问@POST资源时,我收到此错误:
<u>Internal Server Error</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
这是我从Tomcat错误日志
获得的Jan 29, 2014 11:00:21 AM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.myProj.jsp.rest.Track, and Java type class com.gsipartners.apimgmtut
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.myProj.jsp.rest.Track, and Java type class com.myProj.jsp.rest.Track, and MIM
... 24 more
Track类
public class Track {
String title;
String singer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}
答案 0 :(得分:1)
您应该使用Track
注释注释您的JAXB
类,并且Jersey将使用其MessageBodyWriter将返回的实体(轨道)序列化/编组到响应输出流中。或者,您需要为MessageBodyWriter
类型提供自己的Track
实现,以在响应输出流中编写序列化表单。否则,泽西岛不知道该怎么做。