2014-10-04 80 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); 
}