在使用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 变量服务端可以正常拿到值