CXF JAXRS - 如何POST多个参数

dom*_*ino 5 java web-services cxf jax-rs

如何在POST请求中的请求正文中发送多个参数?

@POST
@Consumes("multipart/form-data")
@Produces("application/json")
public String addForm1(@FormParam("i1") Integer i1, @FormParam("i2") Integer i2);
Run Code Online (Sandbox Code Playgroud)

上面的代码返回HTTP 415.

@FormParam用错误替换@Multipart结果Resource method has more than one parameter representing a request body,如下所示.

SEVERE: Resource method service.rs.TestService.postData2 has more than one parameter representing a request body
Exception in thread "main" org.apache.cxf.jaxrs.client.ClientWebApplicationException: Resource method service.rs.TestService.postData2 has more than one parameter representing a request body
at org.apache.cxf.jaxrs.client.ClientProxyImpl.reportInvalidResourceMethod(ClientProxyImpl.java:546)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.getParametersInfo(ClientProxyImpl.java:214)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:138)
at $Proxy20.postData2(Unknown Source)
at service.TestServiceClient.main(TestServiceClient.java:82)
Run Code Online (Sandbox Code Playgroud)

另外,为了传递多种复杂类型,例如List<Map<String, String>>' or 'List<MyNestedCustomObject>在POST方法中,我需要做什么?我可以通过使用JAXB和注释来传递这样的参数@XmlJavaTypeAdapter,但我想这在传递多个参数的情况下不起作用?我是否需要定义自己的消息体读者和作者?任何示例代码都很有用.

谢谢

Don*_*ows 1

你不使用@FormParamwith @Consumes; 仅使用@FormParam参数,CXF 将计算出其余部分。

当我传回结果时,我返回一个包含事物列表的带有 JAXB 注解的对象。CXF 内部使用Jettison,它使用这些 JAXB 注释来指导到 JSON 的转换。这对我来说效果很好。