2012-04-04 9 views
6

Ho un servizio maglia e test di unità (utilizzando il client Jersey) che ha lavorato bene con 3 FormDataParams:Distacco più FormDataParams con lo stesso nome di servizio Java Jersey REST

@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 { 

Il codice di test è come questo:

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); 

Il problema si verifica quando lo cambio per supportare più valori per il campo p1. Ho cambiato la parte firma servizio da

@FormDataParam("p1") String v1, 

a

@FormDataParam("p1") List<String> v1, 

ma poi ho

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) 

La domanda è: come faccio a cambiare il codice di lavoro che ho postato qui sopra per consentire valori multipli per il parametro "p1".

+0

qual è la domanda? –

+0

@Alex L'ho esplicitamente dichiarato alla fine ora. – Alb

+0

hai provato a racchiudere il tuo 'Elenco ' in 'GenericEntity'? –

risposta

7

Ti consigliamo di modificare il parametro

@FormDataParam("p1") List<FormDataBodyPart> v1 

e poi tirare le fila fuori come si elabora il codice

for (FormDataBodyPart vPart : v1) { 
    String v = vPart.getValueAs(String.class); 
    ... 

Potreste essere in grado di chiamare solo vPart.toString() pure; questo è il metodo generale.

+0

.toString() non funziona, .getValueAs (String.class) funziona correttamente –

Problemi correlati