MessageBodyProviderNotFoundException:找不到媒体类型=application/json、类型=class java.lang.Long 的 MessageBodyWriter

MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.lang.Long

配置 Moxy 时遇到问题 Json 从 Jackson 功能到 Springboot + Jersey 应用程序的功能。

更改完成 -

在 ResourceConfig 扩展中 Class -

//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)

使用正确的@Produces 注释时,问题已为我解决。

在这种情况下,它曾经是@Produces(MediaType.APPLICATION_JSON)。将其更改为 @Produces(MediaType.TEXT_PLAIN) 解决了问题。

由于输出是 Long,它正在经历 BasicTypesMessageProvider 编写器模型,该模型仅为 "text/plain" 序列化。

@Produces({"text/plain"})
@Consumes({"text/plain"})
@Singleton
final class BasicTypesMessageProvider extends AbstractMessageReaderWriterProvider<Object> {...}