在使用Spring restTemplate類測試url接口的時候,使用get請求發送參數服務器端拿不到請求數據。
請求代碼是這樣的
Map<String, String> map = new HashMap<>();
map.put("p1", "myValue");
String url = "http://localhost:8080/sayHello";
String paramedUrl = "http://localhost:8080/sayHello?p1={p1}";
System.out.println(restTemplate.getForObject(paramedUrl, String.class, map));
url應該是要寫成 paramedUrl 的形式,不是上面url的形式。類似於一個展位符的作用。
@RequestMapping("sayHello")
@ResponseBody
public String sayHello(HttpServletRequest httpServletRequest) {
System.out.println("服務端拿到的是:" + httpServletRequest.getParameter("p1"));
return "helloworld";
}
使用url的變量服務端拿到的是空,而使用paramedUrl 變量服務端可以正常拿到值