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如何在Spring MVC控制器中獲取HttpRequest getEntity數據?
@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");
}
是否有人可以告訴我這是用SpringMVC中休息控制器擺脫「Request.setEntity」數據的正確方法或者我失去了一些東西?
其次,在postman「httpPost」請求中,我將參數(testKey & testValue)作爲標頭或身體?
第三,在不知道httpPost請求中的參數的情況下,我是否可以解析傳入請求並從Spring控制器中提取參數?
感謝@dudel的細節闡述。你能告訴我有沒有什麼辦法可以簡單地打印客戶發送的RAW請求/數據而不需要知道參數或其他東西? –
在這種情況下,您可以使用'HttpServletRequest'中的'getInputStream()'或'getReader()',但請注意只能讀一次。 – dudel
完美。我們可以稍後閱讀請求/數據,如下所述鏈接? http://stackoverflow.com/questions/10457963/spring-rest-service-retrieving-json-from-request –