Search code examples
javarestglassfishjerseyjersey-2.0

Jersey MessageBodyWriter not found for media type=text/plain


I'm trying to follow the Jersey docs to enable a non-200 response if an error occured (https://jersey.java.net/documentation/latest/representations.html#d0e3586)

My code looks like :

@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public ResponseBuilder getData(@FormParam("one") String one,@FormParam("two") String two,@FormParam("three") String three) {
    if(one.isEmpty() || two.isEmpty() || three.isEmpty()) {
        logger.error("Missing params for getData");
        throw new WebApplicationException(501);
    }
    return Response.ok();
}
}

This unfortunately yields the following error :

[2015-02-01T16:13:02.157+0000] [glassfish 4.1] [SEVERE] [] [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1422807182157] [levelValue: 1000] [[ MessageBodyWriter not found for media type=text/plain, type=class org.glassfish.jersey.message.internal.OutboundJaxrsResponse$Builder, genericType=class javax.ws.rs.core.Response$ResponseBuilder.]]


Solution

  • The problem is the return type of your method. It has to be Response instead of ResponseBuilder.

    Change your code to the following and it should work:

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response getData(@FormParam("one") String one,@FormParam("two") String two,@FormParam("three") String three) {
        if(one.isEmpty() || two.isEmpty() || three.isEmpty()) {
            logger.error("Missing params for getData");
            throw new WebApplicationException(501);
        }
        return Response.ok();
    }