如何在 POST 请求的请求正文中发送多个参数?
@POST
@Consumes("multipart/form-data")
@Produces("application/json")
public String addForm1(@FormParam("i1") Integer i1, @FormParam("i2") Integer i2);
以上代码返回 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)
此外,我需要做什么才能传递多个复杂类型,例如 List<Map<String, String>>' or 'List<MyNestedCustomObject>
在 POST 方法中?我可以使用 JAXB
传递此类参数并用 @XmlJavaTypeAdapter
注释,但我想这在传递多个参数的情况下不起作用?我是否需要定义自己的消息正文读者和作者?任何示例代码都会很有用。
谢谢
最佳答案
我想出了一个方法来做到这一点(见下面的代码)。但是,如果您知道更好的方法,最好不使用“附件”的概念,而是直接使用 jaxrs:client 而不是 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<Attachment> atts = new LinkedList<Attachment>();
atts.add(new Attachment("testItem1", "application/json", t1));
atts.add(new Attachment("testItem2", "application/json", t2));
javax.ws.rs.core.Response s = client.postCollection(atts, Attachment.class);
System.out.println(s.getStatus());
关于java - CXF JAXRS - 如何 POST 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623616/