1
private ArrayList<NameValuePair> mParams;
HttpClient client = new DefaultHttpClient();

mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("testKey", "John"));
mParams.add(new BasicNameValuePair("testSerial", "003-100"));
HttpPost request = new HttpPost("http://localhost:8080/test/getRequiredEnv");

request.setEntity(new UrlEncodedFormEntity(mParams, HTTP.UTF_8));
HttpResponse response = client.execute(request);

// TestController.java

@RestController
public class TestController {   

private static final Logger logger = Logger.getLogger(TestController.class);

@RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public @ResponseBody ResponseInfo getRequiredEnv(
                @RequestParam("testKey") String testKey, 
                @RequestParam("testValue") String testValue, 
                @RequestHeader HttpHeaders headers) {

    logger.info("Test Key [" + testKey + "]");
    logger.info("Test Value [" + testValue + "]");

    return new TestResponseInfo("0001", "ABC");
}

Can someone please tell me is this the correct way to get data from 'Request.setEntity' in SpringMVC rest controller or I am missing something?

Secondly, in postman "httpPost" request I pass the parameters (testKey & testValue) as headers or as body?

Thirdly, without knowing the parameters in httpPost request can I able to parse the incoming request and extract the parameters from it in Spring controller?

alexbt
  • 16,415
  • 6
  • 78
  • 87
Hassan Kashif
  • 435
  • 1
  • 7
  • 26

1 Answers1

1

First of all it would be good to know the content-type of the request that is sent.

So I guess you want to get the body of the request. To get all request parameters if you don't know the parameter names beforehand you can use @RequestParam with type Map<String, String> to get all params:

@RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public @ResponseBody ResponseInfo getRequiredEnv(
            @RequestParam Map<String, String> allParams,
            @RequestHeader HttpHeaders headers)

But I am not sure if this works because it also depends on the content-type. E.g. for form data (application/x-www-form-urlencoded) have a look at the Spring documentation about @RequestBody which states about one of the default message converters FormHttpMessageConverter:

FormHttpMessageConverter converts form data to/from a MultiValueMap.

So try:

@RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public @ResponseBody ResponseInfo getRequiredEnv(
            @RequestBody MultiValueMap<String, String>,
            @RequestHeader HttpHeaders headers)

Alternatively there is also HttpServletRequest.getParameterMap() which gets you a Map. You can get the request by just including HttpServletRequest request as a method argument.

If you know the paramters beforehand, annotating your POJO that resembles the form data with @ModelAttribute should also work like so:

@RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public @ResponseBody ResponseInfo getRequiredEnv(
        @@ModelAttribute Test myTestPojo,
        @RequestHeader HttpHeaders headers)

Or you could also send data as application/json and when including jackson as a dependency, @Requestbody will map your data to a POJO. Have a look at e.g. Spring JSON request body not mapped to Java POJO.

In regard to your second question httpPost will pass the parameters as body since it is a POST request.

dudel
  • 684
  • 5
  • 15
  • Thanks @dudel for the detail elaboration. Can you tell me is there any way to simply print RAW request/data sent from client without knowing parameters or any other stuff? – Hassan Kashif Oct 23 '16 at 20:38
  • In that case you can use either use `getInputStream()` or `getReader()` from the `HttpServletRequest`, but please note that you can only read it once. – dudel Oct 23 '16 at 21:15
  • Perfect. Can we read the request/data later on as described in below link? http://stackoverflow.com/questions/10457963/spring-rest-service-retrieving-json-from-request – Hassan Kashif Oct 25 '16 at 06:09
  • Hmm, you mean to use @RequestBody and still have the data from the inputstream? That will get more complicated, but could work with a ServletFilter as described in the link. Also this answer may help you, although it is for a different purpose: http://stackoverflow.com/a/4097709/1496158 – dudel Oct 25 '16 at 08:50