Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMVC is not recognizing request body parameters if using PUT

Maybe this is supposed to not work, but at least I'd like to understand why then. I am passing a simple val=somevalue in the PUT body but spring sends back a 400 Bad Request as it does not seem to recognise the val parameter.

Similar request works with POST. Could it be SpringMVC is not recognizing the PUT request body as source for parameters?

Content=-Type is set correctly to application/x-www-form-urlencoded in both cases.

The method that spring refuses to call is this:

@RequestMapping(value = "config/{key}", method = RequestMethod.PUT)
@ResponseBody
public void configUpdateCreate(final Model model, @PathVariable final String key, @RequestParam final String val,
        final HttpServletResponse response) throws IOException
{
    //...
}

For completeness, here is the jquery ajax call. I cannot see anything wrong with that. Client is Firefox 4 or Chrome, both show the same result.

$.ajax({
         url:url,
         type:'PUT',
         data:'val=' + encodeURIComponent(configValue),
         success: function(data) {...}
       });      

Any ideas?

like image 367
Sven Haiges Avatar asked May 05 '11 07:05

Sven Haiges


People also ask

Can we use @RequestBody and @RequestParam together?

nothing. So it fails with 400 because the request can't be correctly handled by the handler method.

Is @RequestBody required with @RestController?

If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.

Can we use @RequestBody with get in Spring boot?

In this article, we will discuss how to get the body of the incoming request in the spring boot. @RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project.

Is @RequestBody optional?

With Spring's latest version, if you use @RequestBody annotation, it makes client to send body all the time without making it optional.


2 Answers

I don't know of a work around at this point, but here is the bug report that is a "Won't Fix." I've been fighting the same issue

https://jira.springsource.org/browse/SPR-7414

Update: Here is my fix. I'm using RequestBody annotation. Then using MultiValueMap.

http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/mvc.html#mvc-ann-requestbody

@RequestMapping(value = "/{tc}", method = RequestMethod.PUT) 
public void update(@PathVariable("tc") final String tc, 
@RequestBody MultiValueMap<String,String> body, HttpServletResponse response) {

    String name = body.getFirst("name");
// more code
}
like image 107
mrmanly Avatar answered Oct 20 '22 00:10

mrmanly


Since Spring 3.1, this is resolved using org.springframework.web.filter.HttpPutFormContentFilter.

<filter>
    <filter-name>httpPutFormContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>httpPutFormContentFilter</filter-name>
    <servlet-name>rest</servlet-name>
</filter-mapping>
like image 42
K. Siva Prasad Reddy Avatar answered Oct 19 '22 23:10

K. Siva Prasad Reddy