如何在POST请求中的请求正文中发送多个参数?
@POST
@Consumes("multipart/form-data")
@Produces("application/json")
public String addForm1(@FormParam("i1") Integer i1,@FormParam("i2") Integer i2);
上面的代码返回HTTP 415.
用@Multipart替换@FormParam会导致Resource方法有多个表示请求正文错误的参数,如下所示.
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)
此外,我需要做什么才能传递多个复杂类型,例如List< Map< String,String>>’或’List< MyNestedCustomObject>在POST方法?我可以通过使用JAXB并使用@XmlJavaTypeAdapter注释它来传递这样的参数,但我想这在传递多个参数的情况下不起作用?我是否需要定义自己的消息体阅读器&作家呢?任何示例代码都很有用.
谢谢
最佳答案
我想办法做到这一点(见下面的代码).但是如果你知道更好的方法,最好不使用“附件”的概念并使用jaxrs:客户端而不是WebClient,请告诉我.
服务:
@POST
@Path("/postdata3")
@Consumes("multipart/mixed")
@Produces("application/json")
public String postData3(@Multipart(value = "testItem1",type = "application/json") TestItem t1,@Multipart(value = "testItem2",type = "application/json") TestItem t2
);
客户:
WebClient client = WebClient.create("http://myserver/services/test/postdata3");
client.type("multipart/mixed").accept("application/json");
List