Apache CXF 默认 POST 带有 Jackson 的请求正文
Apache CXF default POST request body with Jackson
在 Apache CXF JAX-RS project I'm working on, I've configured the JSON provider to be Jackson.
这通常有效,但我希望 POST
请求主体始终不是 null
,这样如果客户端发送带有空主体的请求(没有 JSON {}
),我仍然会得到一个默认的 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
之间做出选择
答案并不明显。 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
将实体主体映射到 Java
T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders,
InputStream entityStream)
throws IOException, WebApplicationException
Read a type from the InputStream.
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.
我不清楚"no zero-length entity"是什么意思。无论如何,这是实体提供者的责任,在您的情况下是 Jackson。
Jackson 团队的 Read this post 正在讨论如何处理 JacksonJsonProvider
MessageBodyReader
中的零长度实体
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 中添加了一个新参数
JaxRSFeature.ALLOW_EMPTY_INPUT
使用默认值 true
以与以前的版本兼容。禁用时会引发错误。 NoContentException
对于 JAX-RS 2.x 和 IOException
以及 1.x
在 Apache CXF JAX-RS project I'm working on, I've configured the JSON provider to be Jackson.
这通常有效,但我希望 POST
请求主体始终不是 null
,这样如果客户端发送带有空主体的请求(没有 JSON {}
),我仍然会得到一个默认的 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
之间做出选择答案并不明显。 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
将实体主体映射到 Java
T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException
Read a type from the InputStream.
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.
我不清楚"no zero-length entity"是什么意思。无论如何,这是实体提供者的责任,在您的情况下是 Jackson。
Jackson 团队的Read this post 正在讨论如何处理 JacksonJsonProvider
MessageBodyReader
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 中添加了一个新参数
JaxRSFeature.ALLOW_EMPTY_INPUT
使用默认值 true
以与以前的版本兼容。禁用时会引发错误。 NoContentException
对于 JAX-RS 2.x 和 IOException
以及 1.x