I\'m having a problem with Spring and a post request. I\'m setting up an controller method for an Ajax call, see the method definition below
@RequestMapping
The problem turned out to be the way I was calling the method. My ajax code was passing all the parameters in the request body and not as request parameters, so that's why my @RequestParam
parameters were all empty. I changed my ajax code to:
$.ajax({
type: 'POST',
url: "/comments/add.page?uuid=${param.uuid}&type=${param.type}",
data: text,
success: function (data) {
//
}
});
I also changed my controller method to take the text from the request body:
@RequestMapping(value = "add.page", method = RequestMethod.POST)
@ResponseBody
public Object createComment(
@RequestParam(value = "uuid", required = false) String entityUuid,
@RequestParam(value = "type", required = false) String entityType,
@RequestBody String text,
HttpServletResponse response) {
And now I'm getting the parameters as I expect.