将Moxy Json功能从Jackson功能配置到Springboot + Jersey应用程序时遇到的问题。
已完成更改-
在ResourceConfig扩展类中-
//register(JacksonJsonProvider.class); #Commented
register(MoxyJsonFeature.class);
在Pom.xml文件-
中添加了以下内容<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.23.2</version>
</dependency>
API
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
@Api(value = "xyz")
public class XyzController {
@GET
@Path(Urls.XYZ)
@ApiOperation(value = "xyz", notes = "xyz", nickname = "xyz")
public Long xyz(@Valid @BeanParam final FetchParams fetchParams) {
return testService.xyz(fetchParams);
}
}
API面临的异常-
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.lang.Long, genericType=class java.lang.Long.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86)
答案 0 :(得分:0)
使用正确的@Produces注释时,问题已为我解决。
在这种情况下,它曾经是@Produces(MediaType.APPLICATION_JSON)。将其更改为@Produces(MediaType.TEXT_PLAIN)解决了该问题。
由于输出为Long,因此它将通过BasicTypesMessageProvider
写程序模型,该模型仅针对“文本/纯文本”进行序列化。
@Produces({"text/plain"})
@Consumes({"text/plain"})
@Singleton
final class BasicTypesMessageProvider extends AbstractMessageReaderWriterProvider<Object> {...}