I have two Spring MVC controller methods. Both receive the same data in the request body (in the format of an HTLM POST
form: version=3&name=product1&id=2
), but one method handles PUT
requests and another DELETE
:
@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.PUT)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {
//...
}
@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.DELETE)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {
//...
}
In the first method, all fields of the product
argument are correctly initialised. In the second, only the id
field is initialised. Other fields are null
or 0. (id
is, probably, initialised because of the id
path variable).
I can see that the HttpServletRequest
object contains values for all fields in the request body (version=3&name=product1&id=2
). They just are not mapped to the fields of the product
parameter.
How can I make the second method work?
I also tried to use the @RequestParam
annotated parameters. In the method that handles PUT
requests, it works. In the DELETE
method, I get an exception: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'version' is not present
.
I need to pass data in the body of DELETE
requests because the data contain a row version which is used for optimistic locking.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…