找不到媒体类型= text / plain的Jersey MessageBodyWriter

时间:2015-02-01 16:25:49

标签: java rest glassfish jersey jersey-2.0

如果发生错误(https://jersey.java.net/documentation/latest/representations.html#d0e3586

,我正在尝试关注Jersey文档以启用非200响应

我的代码如下:

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

遗憾的是,这会产生以下错误:

  

[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] [[     找不到媒体类型= text / plain的MessageBodyWriter,类型= class org.glassfish.jersey.message.internal.OutboundJaxrsResponse $ Builder,genericType = class javax.ws.rs.core.Response $ ResponseBuilder。]]

1 个答案:

答案 0 :(得分:7)

问题是您的方法的返回类型。它必须是Response而不是ResponseBuilder

将您的代码更改为以下内容,它应该有效:

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