javaspring-bootjacksonmoxyspring-jersey

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


Faced issue while configuring Moxy Json Feature from Jackson feature to Springboot + Jersey Application.

Changes done -

In ResourceConfig Extended Class -

//register(JacksonJsonProvider.class); #Commented
register(MoxyJsonFeature.class);

Added the following in Pom.xml file -

<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);
    }
}

Exception faced for 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)

Solution

  • Issue solved for me when using proper @Produces annotation.

    In this case, it used to be @Produces(MediaType.APPLICATION_JSON). Changing it to @Produces(MediaType.TEXT_PLAIN) solved the issue.

    Since the output is Long, it is going through BasicTypesMessageProvider writer model which serializes only for "text/plain".

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