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

标签 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] [[ 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.]]

最佳答案

问题在于您的方法的返回类型。它必须是 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();
}

关于java - 找不到媒体类型 Jersey MessageBodyWriter = text/plain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28264911/

相关文章:

java - 从哈希表中删除元素?

java - 不同的java包和可见性

python - url_for 迁移到 Flask Restful

java - 在 domain.xml 中添加 SSL keystore 密码作为别名

java - ActiveMQ 故障转移传输 - 为什么有这么多连接?

java - 多线程死锁

java - 使用 jQuery 动态更改输入值不会操作 JSF 组件的 ajax onchange 事件

debugging - 如何从中间Web API服务捕获HTTP流量?

javascript - 我如何使用 "grant_type=client_credentials"发送 HTTP header

java - 在 GlassFish 2.x 中启用垃圾回收日志记录