2014-10-04 29 views
1

我試圖寫一個REST服務的測試:如何測試使用HttpServletRequest的休息服務?

我有這樣的POST請求:

@RequestMapping(value = "/test", method = RequestMethod.POST) 
public String test(HttpServletRequest request) { 
    String username = request.getParameter("username"); 
    ... 
} 

我試圖用RestTemplate測試它,但我找不到任何例如我可以在HttpServletRequest中設置用戶名。

有沒有解決這個問題的方法?

回答

3
public String postToTest(String username) { 
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); 
    map.add("username", username); 
    return restTemplate.postForObject("http://localhost:8080/soa-server/user/", map, String.class); 
}