一直在用restTemplate你是否真搞清楚了

45次阅读

共计 17294 个字符,预计需要花费 44 分钟才能阅读完成。

发送 GET 请求

<T> ResponseEntity<T> getForEntity(URI var1, Class<T> var2)

/*client*/
@Test
public void testRest(){RestTemplate restTemplate = new RestTemplate();
      String getUrl = "http://localhost:8083/mock/test/get" + "/" + "helloworld";
      ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class);
      log.info(entity.toString());
      log.info(entity.getHeaders().toString());
      log.info(entity.getBody().toString());
}

客户端输出日志:
2020-05-27 09:14:44 [main] INFO AppTest -<200,Student(name= 王杰, age=28),[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:14:44 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]>
2020-05-27 09:14:44 [main] INFO AppTest -[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:14:44 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]
2020-05-27 09:14:44 [main] INFO AppTest -Student(name= 王杰, age=28)

/*server*/
@RestController
@Slf4j
public class Demo {@GetMapping("/test/get/{name}")
    public JSONObject get(@PathVariable("name") String username){log.debug("get server 被调用");
        log.info(username);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }
}

服务端输出日志:
2020-05-27 09:14:44 [http-nio-8083-exec-3] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 09:14:44 [http-nio-8083-exec-3] INFO com.ai.mock.rests.Demo -helloworld

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)

/*client*/
@Test
public void testRest2(){RestTemplate restTemplate = new RestTemplate();
    String getUrl = "http://localhost:8083/mock/test/get2?name={name}&age={age}";
    Map<String,Object> map = new HashMap<>();
    map.put("name","皮皮虾");
    map.put("age",3);
    ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,map);
    log.info(entity.toString());
    log.info(entity.getHeaders().toString());
    log.info(entity.getBody().toString());
    /* 验证 entity 中放的就是 Student 对象 */
    Student s = entity.getBody();
    log.info(s.getName());
    log.info(s.getAge().toString());
}

客户端输出日志:
2020-05-27 09:26:34 [main] INFO AppTest -<200,Student(name= 王杰, age=28),[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:26:34 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]>
2020-05-27 09:26:34 [main] INFO AppTest -[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:26:34 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]
2020-05-27 09:26:34 [main] INFO AppTest -Student(name= 王杰, age=28)
2020-05-27 09:26:34 [main] INFO AppTest - 王杰
2020-05-27 09:26:34 [main] INFO AppTest -28

/*server*/
@GetMapping("/test/get2")
public JSONObject get2(@RequestParam("name") String username,@RequestParam("age") Integer age){log.debug("get server 被调用");
    log.info("username=" + username);
    log.info("age=" + age);
    JSONObject result = new JSONObject();
    result.put("name","王杰");
    result.put("age",28);
    return result;
}

服务端输出日志:
2020-05-27 09:26:34 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 09:26:34 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -username= 皮皮虾
2020-05-27 09:26:34 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -age=3

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object… uriVariables)

    /*client*/
    @Test
    public void testRest3(){RestTemplate restTemplate = new RestTemplate();
        String getUrl = "http://localhost:8083/mock/test/get3?name={name}";
        String name = "hello world";
        ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,name);
        Student s = entity.getBody();
        log.info(s.getName());
        log.info(entity.toString());
        log.info(entity.getHeaders().toString());
        log.info(entity.getBody().toString());
    }

客户端日志输出:
2020-05-27 09:39:54 [main] INFO AppTest -<200,Student(name= 王杰, age=28),[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:39:54 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]>
2020-05-27 09:39:54 [main] INFO AppTest -[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:39:54 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]
2020-05-27 09:39:54 [main] INFO AppTest -Student(name= 王杰, age=28)

    /*server*/
    @GetMapping("/test/get3")
    public JSONObject get3(@RequestParam("name") String username){log.debug("get server 被调用");
        log.info(username);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志输出:
2020-05-27 09:39:54 [http-nio-8083-exec-10] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 09:39:54 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -hello world
如果 Object 为一个 java 对象,想像 Map 一样传参,行不通!在这里我特么想极力吐槽一波。如果想传 java 对象,得这样传:

    /*client*/
    @Test
    public void testRest4(){RestTemplate restTemplate = new RestTemplate();
        String getUrl = "http://localhost:8083/mock/test/get4?name={student}";
        Student student = new Student("皮皮虾",3);
        ResponseEntity<Student> entity = restTemplate.getForEntity(getUrl,Student.class,student);
        log.info(entity.toString());
        log.info(entity.getHeaders().toString());
        log.info(entity.getBody().toString());
    }

客户端日志输出:
2020-05-27 09:48:35 [main] INFO AppTest -<200,Student(name= 王杰, age=28),[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:48:34 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]>
2020-05-27 09:48:35 [main] INFO AppTest -[Content-Type:”application/json”, Transfer-Encoding:”chunked”, Date:”Wed, 27 May 2020 01:48:34 GMT”, Keep-Alive:”timeout=60″, Connection:”keep-alive”]
2020-05-27 09:48:35 [main] INFO AppTest -Student(name= 王杰, age=28)

    /*server*/
    @GetMapping("/test/get4")
    public JSONObject get4(@RequestParam("name") String name){log.debug("get server 被调用");
        log.info(name);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志输出:
2020-05-27 09:48:34 [http-nio-8083-exec-5] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 09:48:34 [http-nio-8083-exec-5] INFO com.ai.mock.rests.Demo -Student(name= 皮皮虾, age=3)

<T> T getForObject(URI url, Class<T> responseType)

/*client*/
    @Test
    public void testRest5(){RestTemplate restTemplate = new RestTemplate();
        String getUrl = "http://localhost:8083/mock/test/get5/helloworld";
        Student stu = restTemplate.getForObject(getUrl,Student.class);
        log.info(stu.toString());
    }

客户端输出日志:
2020-05-27 10:10:52 [main] INFO AppTest -Student(name= 王杰, age=28)

/*server*/
    @GetMapping("/test/get5/{name}")
    public JSONObject get5(@PathVariable("name") String username){log.debug("get server 被调用");
        log.info(username);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端输出日志:
2020-05-27 10:10:52 [http-nio-8083-exec-1] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 10:10:52 [http-nio-8083-exec-1] INFO com.ai.mock.rests.Demo -helloworld

/*client*/
    @Test
    public void testRest6(){RestTemplate restTemplate = new RestTemplate();
        String getUrl = "http://localhost:8083/mock/test/get6?name={name}&age={age}";
        Map<String,Object> map = new HashMap<>();
        map.put("name","皮皮虾");
        map.put("age",3);
        Student s = restTemplate.getForObject(getUrl,Student.class,map);
        log.info(s.toString());
    }

客户端输出日志:
2020-05-27 10:14:47 [main] INFO AppTest -Student(name= 王杰, age=28)

/*server*/
    @GetMapping("/test/get6")
    public JSONObject get6(@RequestParam("name") String username,@RequestParam("age") Integer age){log.debug("get server 被调用");
        log.info("username=" + username);
        log.info("age=" + age);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端输出日志:
2020-05-27 10:14:47 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 10:14:47 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -username= 皮皮虾
2020-05-27 10:14:47 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -age=3

<T> T getForObject(String url, Class<T> responseType, Object… uriVariables)

/*client*/
    @Test
    public void testRest7(){RestTemplate restTemplate = new RestTemplate();
        String getUrl = "http://localhost:8083/mock/test/get7?name={stu}";
        Student stu = new Student("皮皮虾",3);
        Student s = restTemplate.getForObject(getUrl,Student.class,stu);
        log.info(s.toString());
    }

客户端日志输出:
2020-05-27 10:22:29 [main] INFO AppTest -Student(name= 王杰, age=28)

/*server*/
    @GetMapping("/test/get7")
    public JSONObject get7(@RequestParam("name") String name){log.debug("get server 被调用");
        log.info(name);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志输出:
2020-05-27 10:25:50 [http-nio-8083-exec-3] DEBUG com.ai.mock.rests.Demo -get server 被调用
2020-05-27 10:25:50 [http-nio-8083-exec-3] INFO com.ai.mock.rests.Demo -Student(name= 皮皮虾, age=3)

GET 小结

看了前面的一堆测试代码,不禁提问:你怎么这么唇笔啊?server 端不会用 JSONObject 来接受 MAP 或者 java 对象吗?
答案是:不可以!特么的设计者的思想就是 GET 方式 传参 就从 URL 里面取值。不管你是从 path 还是从 url 参数里面都可以。就是不可以从 body 里面取。
getForEntity 与 getForObject 的区别是 getForEntity 返回的数据中包含了请求头、请求体,而 getForObject 只有体。

发送 POST 请求

POST 提交方式主要有两种:form 表单提交、payload 提交。
这里给出鄙人的一点浅薄的见解:

  • form 表单提交: 无论是后端通过 restTemplate 还是前端人员通过 ajax 发送的请求,请求头中 Content-Type: application/x-www-form-urlencoded; charset=UTF-8。server 端获取参数值,需要使用 RequestParmas, 不能使用 RequestBody 获取到。
  • payload 提交:无论是后端通过 restTemplate 还是前端人员通过 ajax 发送的请求,请求头中 Content-Type: application/json;charset=UTF-8。server 端获取参数值,需要使用 RequestBody, 不能使用 RequestParams 获取到。

<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)

<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) form 提交

    /*client*/
    /* 表单提交 Form Data 服务端只能用 RequestParams 取值 不能用 RequestBody*/
    /*Content-Type: application/x-www-form-urlencoded; charset=UTF-8*/
    @Test
    public void testRest8(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("name","皮皮虾");
        params.add("age",3);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(params,headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class);
        log.info(responseEntity.getBody());
    }

客户端日志:
2020-05-28 16:06:54 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    /*server*/
    @PostMapping("/test/post")
    public JSONObject post(@RequestParam("name") String name,@RequestParam("age") Integer age){log.debug("post server 被调用");
        log.info(name);
        log.info(age.toString());
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:06:54 [http-nio-8083-exec-4] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:06:54 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo - 皮皮虾
2020-05-28 16:06:54 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -3

<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) payload 提交

    /*<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)*/
    /*payload 提交 */
    /* 参数放在 body requestParams 取不到 */
    /*Content-Type: application/json;charset=UTF-8*/
    @Test
    public void testRest9(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post2";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("name","皮皮虾");
        params.add("age",3);
        /* 此处注销的代码块 也可以作为请求体参数传递 */
        /*Map<String,Object> params = new HashMap<>();
        params.put("name","皮皮虾");
        params.put("age",3);*/
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Map> entity = new HttpEntity<>(params,headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class);
        log.info(responseEntity.getBody());
    }

客户端日志:
2020-05-28 16:12:35 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    /*server*/
    @PostMapping("/test/post2")
    public JSONObject post2(@RequestBody JSONObject jsonObject){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:12:35 [http-nio-8083-exec-6] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:12:35 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -{“name”:[“ 皮皮虾 ”],”age”:[3]}

<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)

这里就基于 payload 方式举例子了,也可以基于 form data 方式。

    /* 除了请求头里面传值 还可以在 请求参数里面传值 需要显示申明 请求体用 RequestBody 获取 请求头用 RequestParam 获取 */
    @Test
    public void testRest10(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post3?sex={sex}";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("name","皮皮虾");
        params.add("age",3);
        Map<String,Object> uriParam = new HashMap<>();
        uriParam.put("sex","man");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Map> entity = new HttpEntity<>(params,headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class,uriParam);
        log.info(responseEntity.getBody());
    }

客户端日志:
2020-05-28 16:18:32 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    @PostMapping("/test/post3")
    public JSONObject post2(@RequestBody JSONObject jsonObject,@RequestParam("sex") String sex){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        log.info(sex);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

server 端日志:
2020-05-28 16:18:32 [http-nio-8083-exec-7] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:18:32 [http-nio-8083-exec-7] INFO com.ai.mock.rests.Demo -{“name”:[“ 皮皮虾 ”],”age”:[3]}
2020-05-28 16:18:32 [http-nio-8083-exec-7] INFO com.ai.mock.rests.Demo -man

<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object… uriVariables)

这里就基于 payload 方式举例子了,也可以基于 form data 方式。

    @Test
    public void testRest11(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post4?stu={stu}";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("name","皮皮虾");
        params.add("age",3);
        Student stu = new Student("王杰",20);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Map> entity = new HttpEntity<>(params,headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,entity,String.class,stu);
        log.info(responseEntity.getBody());
    }

客户端日志:
2020-05-28 16:22:16 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    @PostMapping("/test/post4")
    public JSONObject post4(@RequestBody JSONObject jsonObject,@RequestParam("stu") String stu){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        log.info(stu);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:22:16 [http-nio-8083-exec-10] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:22:16 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -{“name”:[“ 皮皮虾 ”],”age”:[3]}
2020-05-28 16:22:16 [http-nio-8083-exec-10] INFO com.ai.mock.rests.Demo -Student(name= 王杰, age=20)
postForEntity 小结:如果是 form data 请求 需要设置请求头的 ContentType 为 MediaType.APPLICATION_JSON_UTF8 且将参数 放入 MultiValueMap 中 不可以使用普通 Map,同时需要将请求体 请求头放入 HttpEntity 中之后,发起请求。服务端接受参数使用 RequestParam。
如果是 payload 请求 普通 Map 也可以,需要将 CententType 设置为 MediaType.APPLICATION_JSON_UTF8。服务端接受参数使用 ReuqestBody.

<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)

    /* 默认 payload 方式 */
    @Test
    public void testRest12(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post2";
        Map<String,Object> params = new HashMap<>();
        params.put("name","皮皮虾");
        params.put("age",3);
        String result = restTemplate.postForObject(url,params,String.class);
        log.info(result);
    }

客户端日志:
2020-05-28 16:26:08 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    @PostMapping("/test/post2")
    public JSONObject post2(@RequestBody JSONObject jsonObject){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:26:08 [http-nio-8083-exec-2] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:26:08 [http-nio-8083-exec-2] INFO com.ai.mock.rests.Demo -{“name”:” 皮皮虾 ”,”age”:3}

<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)

    @Test
    public void testRest13(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post3?sex={sex}";
        Map<String,Object> params = new HashMap<>();
        params.put("name","皮皮虾");
        params.put("age",3);
        Map<String,Object> uriParam = new HashMap<>();
        uriParam.put("sex","man");
        String result = restTemplate.postForObject(url,params,String.class,uriParam);
        log.info(result);
    }

客户端日志:
2020-05-28 16:28:51 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    @PostMapping("/test/post3")
    public JSONObject post2(@RequestBody JSONObject jsonObject,@RequestParam("sex") String sex){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        log.info(sex);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:28:51 [http-nio-8083-exec-4] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:28:51 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -{“name”:” 皮皮虾 ”,”age”:3}
2020-05-28 16:28:51 [http-nio-8083-exec-4] INFO com.ai.mock.rests.Demo -man

<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object… uriVariables)

    @Test
    public void testRest14(){RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8083/mock/test/post4?stu={stu}";
        Map<String,Object> params = new HashMap<>();
        params.put("name","皮皮虾");
        params.put("age",3);
        Student stu = new Student("王杰",20);
        String result = restTemplate.postForObject(url,params,String.class,stu);
        log.info(result);
    }

客户端日志:
2020-05-28 16:29:42 [main] INFO AppTest -{“name”:” 王杰 ”,”age”:28}

    @PostMapping("/test/post4")
    public JSONObject post4(@RequestBody JSONObject jsonObject,@RequestParam("stu") String stu){log.debug("post server 被调用");
        log.info(jsonObject.toJSONString());
        log.info(stu);
        JSONObject result = new JSONObject();
        result.put("name","王杰");
        result.put("age",28);
        return result;
    }

服务端日志:
2020-05-28 16:29:42 [http-nio-8083-exec-6] DEBUG com.ai.mock.rests.Demo -post server 被调用
2020-05-28 16:29:42 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -{“name”:” 皮皮虾 ”,”age”:3}
2020-05-28 16:29:42 [http-nio-8083-exec-6] INFO com.ai.mock.rests.Demo -Student(name= 王杰, age=20)

exchange

至于 exchange 如果知道上面的用法之后,只需要在参数请求方法中 添加一个 HttpMethod 就 OK。不再赘述。

正文完
 0