Moxy不能将List <string>编组为JSON </string>

时间:2015-01-29 19:40:07

标签: java json jersey moxy

我有一个使用Jersey 2.6框架运行servlet的Tomcat服务器。在servlet中,我使用Jackson作为JSON&lt; - &gt; POJO编组。我最近了解到了Moxy并试图改用它,因为它似乎是泽西岛的首选。

目前,我的POM中有以下内容:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.6</version>
</dependency>

我的一个RESTful服务如下:

 @GET
 @Path("threatSeverities")
 @Produces(MediaType.APPLICATION_JSON)
 public List<String> getThreatSeverities() {
     return dc.selectAllSeverities();
 }

这与杰克逊合作得很好。当我改为Moxy时,单元测试如下:

 @Test
 public void testThreatSeverities() {
     final Response response = target("threatSeverities").request().get();
     List<String> severities = response.readEntity(new GenericType<List<String>> () {});
     assertNotNull(severities);
 }

会产生以下错误:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=interface java.util.List, genericType=java.util.List<java.lang.String>.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:225)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:812)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:377)
at org.glassfish.jersey.client.InboundJaxrsResponse$2.call(InboundJaxrsResponse.java:122)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:398)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:119)
at com.hp.it.scv.mobile.rest.RestTest.testThreatSeverities(RestTest.java:85)
at (... truncated)

有趣的是要注意,即使控制器方法getThreatSeverities有一个@Produces(MediaType.APPLICATION_JSON),似乎Moxy(或其他我无法看到的东西)正在改变Content-Type单元测试中对text/plain的响应的标题。这一切都不是杰克逊发生的。

还有一点需要注意,这似乎与List<String>个对象有关。 Moxy marshalls POJO列表没有问题。

  1. Moxy本身是否支持将List<String>个对象编组为JSON字符串?似乎答案是否定的。我无法在网上找到确凿的答案。我想知道是否需要为MessageBodyReader个对象编写自己的MessageBodyWriterList<String>
  2. 有人知道在使用Moxy时可能会将Content-Type标题更改为text/plain吗?我怀疑它是Moxy,也许是因为它看到我试图返回一个简单的字符串列表。

  3. 更新(2015年2月2日):

    正如 peeskillet 所指出的,返回的响应是HTTP 500.我能够从响应中提取堆栈跟踪并给出以下内容:

    Stack trace: javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:810)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:263)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:363)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:217)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Unknown Source)
    Caused by: javax.xml.bind.MarshalException - with linked exception:
    [Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: An error occurred marshalling the object
    Internal Exception: Exception [EclipseLink-25007] (Eclipse Persistence  Services - 2.5.0.v20130507-3faac2b):  org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: A descriptor for class java.lang.String was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.]
    at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:403)
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:808)
    ... 25 more
    Caused by: Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: An error occurred marshalling the object
    Internal Exception: Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: A descriptor for class java.lang.String was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.
    at org.eclipse.persistence.exceptions.XMLMarshalException.marshalException(XMLMarshalException.java:97)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:911)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:848)
    at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:401)
    ... 26 more
    Caused by: Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: A descriptor for class java.lang.String was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.
    at org.eclipse.persistence.exceptions.XMLMarshalException.descriptorNotFoundInProject(XMLMarshalException.java:139)
    at org.eclipse.persistence.internal.oxm.Context$ContextState.getSession(Context.java:143)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:787)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:1)
    at org.eclipse.persistence.internal.oxm.Context.getSession(Context.java:451)
    at org.eclipse.persistence.oxm.XMLContext.getSession(XMLContext.java:356)
    at org.eclipse.persistence.oxm.XMLContext.getSession(XMLContext.java:1)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:568)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1096)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:869)
    ... 28 more
    

    Exception Description: A descriptor for class java.lang.String was not found in the project.开始,如果没有手动编码,似乎{J}编组就不可能{J}编组。

1 个答案:

答案 0 :(得分:4)

MOXy代码中存在一些不明确的限制(moxy / org.eclipse.persistence.moxy / src / org / eclipse / persistence / jaxb / rs / MOXyJsonProvider.java),这阻止了它处理&#39; java的集合*&#39;类。我已经针对此提出了这个问题。

https://bugs.eclipse.org/bugs/show_bug.cgi?id=459227