将具有相同名称的多个FormDataParams发布到java Jersey REST服务

Alb*_*Alb 6 java rest web-services jersey

我有一个球衣服务和单元测试(使用泽西客户端),可以使用3 FormDataParams:

@Path("myService")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@POST
@Produces(MediaType.TEXT_PLAIN)
public Response doService(@FormDataParam("p1") String v1,
                         @FormDataParam("p2") InputStream v2,
                         @FormDataParam("p3") InputStream v3) throws IOException {
Run Code Online (Sandbox Code Playgroud)

测试代码是这样的:

FormDataMultiPart fdmp = new FormDataMultiPart();      
fdmp.field("p1", v1);
fdmp.field("p2", v2);
fdmp.field("p3", v3);
ClientResponse response = service.path("myService").type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN).post(ClientResponse.class, fdmp);
Run Code Online (Sandbox Code Playgroud)

问题是当我更改它以支持p1字段的多个值时.我更改了服务签名部分

@FormDataParam("p1") String v1,
Run Code Online (Sandbox Code Playgroud)

@FormDataParam("p1") List<String> v1,
Run Code Online (Sandbox Code Playgroud)

但后来我明白了

04-Apr-2012 18:56:59 com.sun.grizzly.http.servlet.ServletAdapter doService
SEVERE: service exception:
java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:172)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:265)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:996)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:947)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:938)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:399)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:478)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:663)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
Run Code Online (Sandbox Code Playgroud)

问题是如何更改上面发布的工作代码以允许"p1"参数的多个值.

小智 7

您需要将参数更改为

@FormDataParam("p1") List<FormDataBodyPart> v1
Run Code Online (Sandbox Code Playgroud)

然后在处理代码时关闭Strings

for (FormDataBodyPart vPart : v1) {
    String v = vPart.getValueAs(String.class);
    ...
Run Code Online (Sandbox Code Playgroud)

你也许也可以打电话vPart.toString(); 这是一般方法.