Spring RestTemplate get方式发送数据服务器端拿到为空

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章