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?
nothing. So it fails with 400 because the request can't be correctly handled by the handler method.
If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.
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.
With Spring's latest version, if you use @RequestBody annotation, it makes client to send body all the time without making it optional.
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
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With