泽西,灰熊 - 将Java POJO打包为MediaType.APPLICATION_JSON时消息体编写器异常

时间:2013-12-27 13:20:43

标签: json jersey grizzly

我有一个Grizzly http服务器来实现一个jersey-REST API服务器。我正在尝试为@GET @Produces(MediaType.APPLICATION_JSON)

制作一个休息API

我遇到了像 A message body writer for Java class com.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json was not found

当我将字符串作为JSON返回时,相同的代码正在运行。现在,当我尝试将Java对象作为JSON返回时,它不起作用。在SO中有很多问题,但他们都差不多一年了;我试图实现它们 - 但问题没有解决。我怀疑这可能是由于较新版本的灰熊......任何人都可以帮忙吗?

链接到github项目https://github.com/seshumadhav/java-projects/tree/master/grizzlySpringJersey

从我的pom复制的Imp版本信息:

<!-- Jersey: javax.ws -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-grizzly2</artifactId>
    <version>1.17.1</version>
</dependency>
<!-- >>> For message body exception error. JSONConfiguration.FEATURE_POJO_MAPPING; This did not work -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.17.1</version>
</dependency>

<!--  Grizzly2 -->
<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http-servlet</artifactId>
    <version>2.2.16</version>
</dependency>

下面的详细例外跟踪

Dec 27, 2013 6:25:35 PM org.glassfish.grizzly.servlet.WebappContext deploy
INFO: Starting application [DEMO REST Server WebappContext] ...
Dec 27, 2013 6:25:35 PM org.glassfish.grizzly.servlet.WebappContext log
INFO: [DEMO REST Server WebappContext] Initializing Spring root WebApplicationContext
Dec 27, 2013 6:25:35 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Dec 27, 2013 6:25:35 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri Dec 27 18:25:35 IST 2013]; root of context hierarchy
Dec 27, 2013 6:25:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Dec 27, 2013 6:25:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c893391: defining beans [resourceFoo,springRunner,myBean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Dec 27, 2013 6:25:36 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 421 ms
Dec 27, 2013 6:25:36 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
INFO: Using default applicationContext
Dec 27, 2013 6:25:36 PM com.sun.jersey.spi.spring.container.SpringComponentProviderFactory registerSpringBeans
INFO: Registering Spring bean, resourceFoo, of type com.demo.web.ResourceFoo as a root resource class
Dec 27, 2013 6:25:36 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM'
Dec 27, 2013 6:25:36 PM org.glassfish.grizzly.servlet.WebappContext initServlets
INFO: [DEMO REST Server WebappContext] Servlet [com.sun.jersey.spi.spring.container.servlet.SpringServlet] registered for url pattern(s) [[/*]].
Dec 27, 2013 6:25:36 PM org.glassfish.grizzly.servlet.WebappContext deploy
INFO: Application [DEMO REST Server WebappContext] is ready to service requests.  Root: [/].
Dec 27, 2013 6:25:37 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:3388]
Dec 27, 2013 6:25:37 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
In order to test the server please try the following urls:
http://localhost:3388/smc/time to see time for smc
http://localhost:3388/bsv/time to see time for bsv
Press enter to stop the server...
Dec 27, 2013 6:25:44 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json was not found
Dec 27, 2013 6:25:44 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

Dec 27, 2013 6:25:44 PM 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.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json 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:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:147)
    at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
    at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:252)
    at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
    at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:196)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:175)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547)
    at java.lang.Thread.run(Thread.java:744)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json was not found
    ... 30 more

Dec 27, 2013 6:25:48 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json was not found
Dec 27, 2013 6:25:48 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

Dec 27, 2013 6:25:48 PM 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.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json 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:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:147)
    at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
    at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:252)
    at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
    at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:196)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:175)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547)
    at java.lang.Thread.run(Thread.java:744)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.demo.beans.MyBlob, and Java type class com.demo.beans.MyBlob, and MIME media type application/json was not found
    ... 30 more

1 个答案:

答案 0 :(得分:0)

我建议在你的pom中添加一个JSON提供程序:

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-json</artifactId>
  <version>${jersey.version}</version>
</dependency>

您可能需要启用com.sun.jersey.api.json.POJOMappingFeature。这可以通过编程方式完成:

ResourceConfig rc = // choose your method of setting up your ResourceConfig
rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

或通过web.xml文件:

<init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
</init-param>

查看Jersey 1.17 documentation了解更多详情。

此外,我还是建议尽可能使用Jersey 2。但请注意,如果您决定采用此路线,则上述建议不会直接映射到泽西岛2。