Java 找不到媒体类型的 Jersey MessageBodyWriter=text/plain
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28264911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey MessageBodyWriter not found for media type=text/plain
提问by Little Code
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)
如果发生错误,我正在尝试按照 Jersey 文档启用非 200 响应(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.]]
[2015-02-01T16:13:02.157+0000] [glassfish 4.1] [严重] [] [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.]]
采纳答案by unwichtich
The problem is the return type of your method. It has to be Response
instead of ResponseBuilder
.
问题在于您的方法的返回类型。它必须是Response
而不是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();
}