How to explicitly obtain post data in Spring MVC?
If you are using one of the built-in controller instances, then one of the parameters to your controller method will be the Request object. You can call request.getParameter("value1")
to get the POST (or PUT) data value.
If you are using Spring MVC annotations, you can add an annotated parameter to your method's parameters:
@RequestMapping(value = "/someUrl")
public String someMethod(@RequestParam("value1") String valueOne) {
//do stuff with valueOne variable here
}
Another answer to the OP's exact question is to set the consumes
content type to "text/plain"
and then declare a @RequestBody String
input parameter. This will pass the text of the POST data in as the declared String
variable (postPayload
in the following example).
Of course, this presumes your POST payload is text data (as the OP stated was the case).
Example:
@RequestMapping(value = "/your/url/here", method = RequestMethod.POST, consumes = "text/plain")
public ModelAndView someMethod(@RequestBody String postPayload) {
// ...
}