杰克逊的Apache CXF默认POST请求正文 [英] Apache CXF default POST request body with Jackson

查看:275
本文介绍了杰克逊的Apache CXF默认POST请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在研究的 Apache CXF JAX-RS 项目中,我已将JSON提供程序配置为 Jackson .

In an Apache CXF JAX-RS project I'm working on, I've configured the JSON provider to be Jackson.

这通常可行,但是我希望POST请求正文始终不是null,因此,如果客户端发送的请求正文为空(无JSON {}),我仍然获取默认的POJO.

This generally works, but I'd like the POST request body to always be not null, so that if a client sends a request with an empty body (no JSON {}), I'd still get a default POJO.

例如

CXF侧:

@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path("/foo")
public Response postFoo(FooObj foo) {
     if (foo == null)
         return Response.ok("No foo");
     else
         return Response.ok("Foo found");
}

客户端:

curl -XPOST -H "Content-Type: application/json" "http://localhost/foo"
"No Foo" // Though we'd like to see "Foo found"

推荐答案

不可能使用CXF + Jackson获取带有空响应的默认POJO.您可以在 null或NoContentException

It is not possible to get a default POJO with an empty response using CXF+Jackson. You can decide between null or NoContentException

答案不明显. MessageBodyReader 映射了Java的实体主体

The answer is not obvious. The JAX-RS specification 3.3.2.1 stablish that the conversion between an entity body and a Java type is the responsibility of an entity provider. Interface MessageBodyReader maps the entity body to Java

T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders,
     InputStream entityStream)
       throws IOException, WebApplicationException

从InputStream读取类型.

Read a type from the InputStream.

如果实体输入流为空,则期望阅读器返回零长度实体的Java表示形式,或者如果没有为支持的Java类型定义零长度实体表示,则抛出NoContentException. .如果在阅读服务器请求实体时消息正文阅读器抛出NoContentException,则JAX-RS服务器运行时会自动将NoContentException转换为包装原始NoContentException的BadRequestException,并由注册的异常映射器将其重新抛出以进行标准处理.

In case the entity input stream is empty, the reader is expected to either return a Java representation of a zero-length entity or throw a NoContentException in case no zero-length entity representation is defined for the supported Java type. A NoContentException, if thrown by a message body reader while reading a server request entity, is automatically translated by JAX-RS server runtime into a BadRequestException wrapping the original NoContentException and rethrown for a standard processing by the registered exception mappers.

对于我来说,无零长度实体" 的含义还不清楚.无论如何,这都是实体提供者的责任,在您的情况下是杰克逊.

It is not clear for me what is the meaning of "no zero-length entity". In any case, it is responsability of the entity provider, in your case Jackson.

阅读此帖子杰克逊团队讨论如何处理在JacksonJsonProvider MessageBodyReader

Read this post of Jackson's team discussing about how to deal with zero-length entities in JacksonJsonProvider MessageBodyReader

ProviderBase中的readFrom()方法遇到空流时将返回null.根据MessageBodyReader的javadoc和JSR311的说法,这是不允许的.

The readFrom() method in ProviderBase returns null when it encounters an empty stream. According to both the javadoc for MessageBodyReader, and JSR311, this is not allowed.

Jackson小组认为返回null始终是一个错误,并且阅读了新规范JAX-RS2.0,他们在Jackson 2.4.0中添加了新参数

Jackson team consider returning null always is a bug, and reading the new specification JAX-RS2.0, they add a new parameter in Jackson 2.4.0

JaxRSFeature.ALLOW_EMPTY_INPUT

,默认值true与以前的版本兼容.禁用时将引发错误. NoContentException对于JAX-RS 2.x和IOException与1.x

with default value true to be compatible with previous versions. When disabled it will raise an error. NoContentException for JAX-RS 2.x and IOException with 1.x

这篇关于杰克逊的Apache CXF默认POST请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆