Spring @RequestParam null when sending Parameter in form-data

In my controller when sending parameter as form-data, its receiving as null value. When passing parameter as x-www-form-urlencoded, I am getting the value. My controller is like the following:

@RequestMapping(value = "/getid", method = RequestMethod.POST)
public ServerResponse id(@RequestParam(value = "id", required = false) String id) {...}

Upvotes: 2

Views: 3447

Answers (1)

Periklis Douvitsas
Periklis Douvitsas

Reputation: 2491

If you need to send parameters are form-data then you need to add support for this in Spring, Have a look at spring's documentation http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart

Spring’s built-in multipart support handles file uploads in web applications. You enable this multipart support with pluggable MultipartResolver objects, defined in the org.springframework.web.multipart package. Spring provides one MultipartResolver implementation for use with Commons FileUpload and another for use with Servlet 3.0 multipart request parsing.

By default, Spring does no multipart handling, because some developers want to handle multiparts themselves. You enable Spring multipart handling by adding a multipart resolver to the web application’s context. Each request is inspected to see if it contains a multipart. If no multipart is found, the request continues as expected. If a multipart is found in the request, the MultipartResolver that has been declared in your context is used. After that, the multipart attribute in your request is treated like any other attribute.

This is an example of how to use CommonsMultipartResolver

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>

</bean>

Of course you also need to put the appropriate jars in your classpath for the multipart resolver to work. In the case of the CommonsMultipartResolver, you need to use commons-fileupload.jar.

Upvotes: 1

Related Questions