我正在使用 Maven、Jersey 和 Jetty 服务器来构建 Web 服务接口。
当我在 Eclipse 上开发和测试软件时,一切正常。但是,当我尝试部署项目并向接口发出请求时,服务器报告“未找到 MIME 媒体类型应用程序/xml”。
我尝试了几天来解决问题,发现ServiceFinder(由Jersey提供)可以在Eclipse上找到所有MessageBodyWriter,但不能在命令行中找到。我还检查了所有球衣库是否已经到位,并在 META-INF 中定义。
非常感谢任何帮助!谢谢!
JDK:1.6.0_30 Eclipse:Indigo SR2 Jersey:1.12 码头:6.1.25
服务查找器代码:
ServiceFinder.find(javax.ws.rs.ext.MessageBodyWriter.class, true).toClassArray();
=============== META-INF 创建者:Apache Maven 构建者:Build-Jdk:1.6.0_30 类路径:jersey-servlet-1.12.jar jersey-server- 1.12.jar asm-3.1.jar jersey-core-1.12.jar
============= 在命令行中(只有 ViewableMessageBodyWriter 可用)
GN MBR:class com.sun.jersey.server.impl.template.ViewableMessageBodyWriter CP:co
m.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@28946
139 C:com.sun.jersey.server.impl.template.ViewableMessageBodyWriter@6a7d46b8
============= 在 Eclipse 中(所有球衣供应商都可用)
GN MBR:class com.sun.jersey.server.impl.template.ViewableMessageBodyWriter CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@2fba32e3 C:com.sun.jersey.server.impl.template.ViewableMessageBodyWriter@60039882
GN MBR:class com.sun.jersey.core.impl.provider.entity.StringProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@6962ed8f C:com.sun.jersey.core.impl.provider.entity.StringProvider@31cc3e7c
GN MBR:class com.sun.jersey.core.impl.provider.entity.ByteArrayProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@5dda7d56 C:com.sun.jersey.core.impl.provider.entity.ByteArrayProvider@1af30c34
GN MBR:class com.sun.jersey.core.impl.provider.entity.FileProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@335ac93d C:com.sun.jersey.core.impl.provider.entity.FileProvider@6695c13b
GN MBR:class com.sun.jersey.core.impl.provider.entity.InputStreamProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@d51de22 C:com.sun.jersey.core.impl.provider.entity.InputStreamProvider@4592b04e
GN MBR:class com.sun.jersey.core.impl.provider.entity.DataSourceProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@79ce9102 C:com.sun.jersey.core.impl.provider.entity.DataSourceProvider@41fb404
GN MBR:class com.sun.jersey.core.impl.provider.entity.RenderedImageProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@689258c7 C:com.sun.jersey.core.impl.provider.entity.RenderedImageProvider@4c843d2b
GN MBR:class com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@6002a673 C:com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider@20f1fbb4
GN MBR:class com.sun.jersey.core.impl.provider.entity.FormProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@4dfdf943 C:com.sun.jersey.core.impl.provider.entity.FormProvider@44d0d6fd
GN MBR:class com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@5cf7c5b5 C:com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider@29dafde4
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@ed51dc2 C:com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App@8c5697a
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@4994b329 C:com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text@48e14b1f
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@417e6e7c C:com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General@6c9ce1cb
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@5387b07b C:com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App@2acb4dcf
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@71538614 C:com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text@59b02f33
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@2287c52c C:com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General@9fc1ec5
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@7fa1dd69 C:com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App@7a95626d
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@5bcf59f5 C:com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text@1e69555f
GN MBR:class com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@a1b12ea C:com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General@4feaf536
GN MBR:class com.sun.jersey.core.impl.provider.entity.ReaderProvider CP:com.sun.jersey.core.spi.component.ProviderFactory$SingletonComponentProvider@7776c5d4 C:com.sun.jersey.core.impl.provider.entity.ReaderProvider@5d66341b
...
============= 完整错误堆栈 ==================
23-May-2012 12:05:54 com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type
are:
*/* ->
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
23-May-2012 12:05:54 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.test.TestRest, and Java type class com.test.TestRest, and MIME media type application/xml was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1451)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.test.TestRest, and Java type class com.test.TestRest, and MIME media type application
/xml was not found
... 21 more
============= 启动 Jetty 服务器 ==== 我正在使用 Jetty Web 服务器,并像这样启动服务器:
Context root = new Context(server, "/", Context.SESSIONS);
// Add REST handlers and Singleton providers
ResourceConfig rc = new PackagesResourceConfig("handlerpkg");
// Add the Jersey root servlet holder to the root context
root.addServlet(new ServletHolder(new ServletContainer(rc)), "/*");
server.start();