87

Is there a way to obtain the post data itself? I know spring handles binding post data to java objects. But, given two fields that I want to process, how can I obtain that data?

For example, suppose my form had two fields:

 <input type="text" name="value1" id="value1"/>
 <input type="text" name="value2" id="value2"/>

How would I go about retrieving those values in my controller?

royhowie
  • 11,075
  • 14
  • 50
  • 67

4 Answers4

142

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
}
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
  • 1
    does this work with multple request paramters, it does right ? You don't have to wrap in one object ? http://stackoverflow.com/q/12862320/106261 – NimChimpsky Oct 12 '12 at 16:22
  • 1
    <3 this for times when you don't want/need a Model-backed form (for a search form, or a contact form, etc.) – Don Cheadle Feb 02 '15 at 01:16
42

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) {    
        // ...    
    }
simon
  • 3,380
  • 2
  • 22
  • 23
  • 3
    This is the answer I was looking for. By 'the post data itself' I understand the same. Thanks! – mauromartini Dec 03 '14 at 02:35
  • For multiple post params (such as `value1` and `value2`, this would create one String for both values? Then you would need to parse out the individual values? Why do that when you can specify/obtain each value individually as in the accepted answer? – Don Cheadle Feb 02 '15 at 01:18
  • 1
    @mmcrae - it's simply another way to solve the problem, and perhaps useful to other users who don't have multiple POST params but instead just have a blob of text or binary data. That was my situation, and that is what I was hoping to learn when I clicked on this question. – simon Feb 06 '15 at 20:38
  • 1
    Exactly what I was looking for. It works with consumes="application/json" too if posting json. – Manish May 14 '16 at 05:13
  • Hello. I'm using [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) to test my API. Postman helps me fill the body for POST with data. The problem is that I get 403 when I use `RequestMethod.POST`. I even added `@CrossOrigin(origins = "*")` but I still get the same thing... Don't know what is going on... I have used the same signature as your answer (except that `someMethod` returns a `String` in my case)... – Kurt Miller Apr 03 '18 at 17:00
27

Spring MVC runs on top of the Servlet API. So, you can use HttpServletRequest#getParameter() for this:

String value1 = request.getParameter("value1");
String value2 = request.getParameter("value2");

The HttpServletRequest should already be available to you inside Spring MVC as one of the method arguments of the handleRequest() method.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Hai @BalusC, in my case I have more than 20 variables in request. However I have a bean with all the request param variables. Now I want to get all the values into bean from request instead of getting one by one. We have this feature in struts2 by using "ModelDrivenBean". Do we have any feature like this in spring framework. – vissu Apr 17 '12 at 09:38
  • 2
    Yes, using the @ModelAttribute annotation and a form backing bean. This link describes nicely how to do that: http://viralpatel.net/blogs/2010/07/spring-3-mvc-handling-forms.html –  Apr 18 '12 at 14:13
  • @vissu if you have more than 20 variables you should really look at simplifying – CodeMonkey Oct 12 '16 at 18:38
  • 1
    Hello BalusC. Following [simmon's answer](https://stackoverflow.com/a/25043158/5618563) below I'm using [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) to test my API. Postman helps me fill the body for POST with data. The problem is that I get 403 when I use `RequestMethod.POST`. I even added `@CrossOrigin(origins = "*")` but I still get the same thing... Don't know what is going on... I have used the same signature as your answer (except that `someMethod` returns a `String` in my case)... – Kurt Miller Apr 03 '18 at 17:02
0

You can simply just pass the attribute you want without any annotations in your controller:

@RequestMapping(value = "/someUrl")
public String someMethod(String valueOne) {
 //do stuff with valueOne variable here
}

Works with GET and POST

Suisse
  • 3,467
  • 5
  • 36
  • 59