Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Restful Service + Spring for android, missing parameter while uploading file
    primarykey
    data
    text
    <p>I've had fight with Spring restservice for 2 days and I can't get through file upload.</p> <p>I my controller and its methods is based on guide <a href="http://spring.io/guides/gs/uploading-files/" rel="nofollow">http://spring.io/guides/gs/uploading-files/</a></p> <p>But I had many problems with that. First of all I had to include library: jackson-mapper-asl because my request's were ALWAYS returning an error. I wonder why in Spring Webservice template project this library wasn't added.</p> <p>Since I've added jackson-mapper-asl library my simple GET request were handled properly. But when I implemented method that handles file upload as it was described in this guide it wouldn't work at all.</p> <p>this is my Controller class and its method implementation:</p> <pre><code>@Controller @RequestMapping("/controller") public class GreetingsController { @RequestMapping(value="/upload2", method=RequestMethod.POST) public @ResponseBody String handleFileUpload2(@RequestParam("name") String name, @RequestParam("myfile") MultipartFile file){ if (file != null &amp;&amp; !file.isEmpty()) { try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded"))); stream.write(bytes); stream.close(); return "You successfully uploaded " + name + " into " + name + "-uploaded !"; } catch (Exception e) { return "You failed to upload " + name + " =&gt; " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } } </code></pre> <p>this is my part of android code that makes a request:</p> <pre><code>final String serviceAddress3= "http://someaddress:8080/testappx2/controller/upload2"; String mb05 = sdcardMountPoint + "/DCIM/halfMB.jar"; final RestTemplate restTemplate = new RestTemplate(true); FileSystemResource fsr = new FileSystemResource(mb05); final LinkedMultiValueMap&lt;String, Object&gt; map = new LinkedMultiValueMap&lt;String, Object&gt;(); map.add("name", "superplik"); map.add("myfile", fsr); Thread t = new Thread(new Runnable(){ public void run() { Object result = restTemplate.postForObject(serviceAddress2, map, String.class); } }); t.start(); </code></pre> <p>I use following libraries:</p> <pre><code>* spring-core 3.0.5.RELEASE * spring-web 3.0.5.RELEASE * spring-webmvc 3.0.5.RELEASE * jackson-mapper-asl 1.9.13 </code></pre> <p>I tried to make a POST request with SoapUI and directly from an Android mock APP. The only result I got was: <code>org.springframework.web.client.HttpClientErrorException: 400 Bad Request</code> I decided to dug into my application server logs and I found out that my request is not "complete"</p> <p>Request handle logs below:</p> <pre><code>16:09:46,029 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-2) Begin invoke, caller=null 16:09:46,030 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Bound request context to thread: org.apache.catalina.connector.RequestFacade@6195db1d 16:09:46,032 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) DispatcherServlet with name 'mvc-dispatcher' processing POST request for [/testappx2/controller/upload] 16:09:46,034 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@16321271] in DispatcherServlet with name 'mvc-dispatcher' 16:09:46,035 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (http--0.0.0.0-8080-2) Mapping [/controller/upload] to HandlerExecutionChain with handler [hello.GreetingsController@6ee8560d] and 2 interceptors 16:09:46,037 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@5769a85d] 16:09:46,039 DEBUG [org.apache.tomcat.util.http.Parameters] (http--0.0.0.0-8080-2) Set encoding to ISO-8859-1 16:09:46,040 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present 16:09:46,043 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present 16:09:46,045 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present 16:09:46,047 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling 16:09:46,048 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@6195db1d 16:09:46,049 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Successfully completed request 16:09:46,050 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-2) Publishing event in WebApplicationContext for namespace 'mvc-dispatcher-servlet': ServletRequestHandledEvent: url=[/testappx2/controller/upload]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[20ms]; status=[OK] 16:09:46,051 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-2) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testappx2/controller/upload]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[20ms]; status=[OK] 16:09:46,052 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-2) End invoke, caller=null 16:09:46,052 TRACE [org.jboss.security.SecurityRolesAssociation] (http--0.0.0.0-8080-2) Setting threadlocal:null 16:09:46,052 TRACE [org.jboss.jca.core.api.connectionmanager.ccm.CachedConnectionManager] (http--0.0.0.0-8080-2) popped object: org.jboss.as.connector.deployers.processors.CachedConnectionManagerSetupProcessor$CachedConnectionManagerSetupAction@4bae320 </code></pre> <p>Appserver says that "name" parameter was not passed: <code>[hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present</code></p> <p>The fun thing is (what I have accidentally discovered) that when I omit adding "myfile" parameter to LinkedMultiValueMap It founds it, parses it and then it gets into body of the method.</p> <p>Appserver log:</p> <pre><code>16:52:29,391 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] (http--0.0.0.0-8080-1) Resolving exception from handler [hello.GreetingsController@788a7860]: org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myfile' is not present 16:52:29,391 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http--0.0.0.0-8080-1) Resolving exception from handler [hello.GreetingsController@788a7860]: org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myfile' is not present 16:52:29,392 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling 16:52:29,393 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@8f22ae 16:52:29,393 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Successfully completed request 16:52:29,394 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-1) Publishing event in WebApplicationContext for namespace 'mvc-dispatcher-servlet': ServletRequestHandledEvent: url=[/testappx3/controller/upload2]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[35ms]; status=[OK] 16:52:29,394 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-1) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testappx3/controller/upload2]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[35ms]; status=[OK] 16:52:29,395 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-1) End invoke, caller=null </code></pre> <p>Now appserver says there's no "myfile" parameter.</p> <p>I thought there's something wrong with having declared more than 1 <code>RequestParameter</code> in this method. But tried it with multiple <code>String</code> <code>RequestParameters</code> and that seems to work fine.</p> <p>What am I doing wrong? Am I leprous or smth?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload