找不到媒体类型的 Jersey MessageBodyWriter=text/plain
Jersey MessageBodyWriter not found for media type=text/plain
我正在尝试按照 Jersey 文档启用非 200 响应(如果发生错误)(https://jersey.java.net/documentation/latest/representations.html#d0e3586)
我的代码如下:
@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] [[
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.]]
问题出在您的方法的 return 类型上。它必须是 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();
}
我正在尝试按照 Jersey 文档启用非 200 响应(如果发生错误)(https://jersey.java.net/documentation/latest/representations.html#d0e3586)
我的代码如下:
@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] [[ 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.]]
问题出在您的方法的 return 类型上。它必须是 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();
}