精品RestTemplate工具类及使用

【精品RestTemplate工具类及使用】敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述精品RestTemplate工具类及使用相关的知识,希望能为你提供帮助。
第一步:创建待测试的项目 添加Maven依赖

< dependency> < groupId> org.projectlombok< /groupId> < artifactId> lombok< /artifactId> < version> 1.18.12< /version> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-test< /artifactId> < scope> test< /scope> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-web< /artifactId> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-thymeleaf< /artifactId> < /dependency> < !--RestTemplate 默认使用 jackson 完成 json 序列化和反序列化.--> < dependency> < groupId> com.fasterxml.jackson.core< /groupId> < artifactId> jackson-databind< /artifactId> < /dependency>

application.yml
server: port: 80 servlet: context-path: /trtd

创建实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Dept { private int deptno; private String dname; private String loc; }

创建Controller
@Controller @RequestMapping("/dept3") public class DeptController3 {@ResponseBody @GetMapping("/demo11") public Dept demo11() { Dept dept = new Dept(10, "aa", "aaaaaaaa"); return dept; }@ResponseBody @GetMapping("/demo12") public Dept demo12(String dname, String loc) { Dept dept = new Dept(); dept.setDeptno(new Random().nextInt()); dept.setDname(dname); dept.setLoc(loc); return dept; } @ResponseBody @GetMapping("/demo13") public Dept demo13(@RequestParam("deptno") Integer deptno,String dname, String loc) { Dept dept = new Dept(); dept.setDeptno(deptno); dept.setDname(dname); dept.setLoc(loc); return dept; }///////////////////////////////////////////////////// @ResponseBody @PostMapping("/demo21") public Dept demo21() { Dept dept = new Dept(10, "aa", "aaaaaaaa"); return dept; }@ResponseBody @PostMapping("/demo22") public Dept demo22(String dname, String loc) { Dept dept = new Dept(); dept.setDeptno(new Random().nextInt()); dept.setDname(dname); dept.setLoc(loc); return dept; }@ResponseBody @PostMapping("/demo23") public Dept demo23(@RequestParam("query")String query, @RequestParam("deptno") Integer deptno,String dname, String loc) { System.out.println(query); Dept dept = new Dept(); dept.setDeptno(deptno); dept.setDname(dname); dept.setLoc(loc); return dept; }}

创建进行测试的SpringBoot项目 添加Maven依赖
< dependency> < groupId> org.projectlombok< /groupId> < artifactId> lombok< /artifactId> < version> 1.18.12< /version> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-test< /artifactId> < scope> test< /scope> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-web< /artifactId> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-thymeleaf< /artifactId> < /dependency> < !--RestTemplate 默认使用 jackson 完成 json 序列化和反序列化.--> < dependency> < groupId> com.fasterxml.jackson.core< /groupId> < artifactId> jackson-databind< /artifactId> < /dependency>

创建RestTemplateUtils工具类
/** * RestTemplate 远程调用工具类 */ public class RestTemplateUtils {private static final RestTemplate restTemplate = new RestTemplate(); // ----------------------------------GET-------------------------------------------------------/** * GET请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, Class< T> responseType) { return restTemplate.getForEntity(url, responseType); }/** * GET请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, Class< T> responseType, Object... params) { return restTemplate.getForEntity(url, responseType, params); }/** * GET请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, Class< T> responseType, Map< String, ?> params) { return restTemplate.getForEntity(url, responseType, params); }/** * 带请求头的GET请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, Map< String, String> headers, Class< T> responseType, Object... params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return get(url, httpHeaders, responseType, params); }/** * 带请求头的GET请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, HttpHeaders headers, Class< T> responseType, Object... params) { HttpEntity< ?> requestEntity = new HttpEntity< > (headers); return exchange(url, HttpMethod.GET, requestEntity, responseType, params); }/** * 带请求头的GET请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, Map< String, String> headers, Class< T> responseType, Map< String, ?> params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return get(url, httpHeaders, responseType, params); }/** * 带请求头的GET请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> get(String url, HttpHeaders headers, Class< T> responseType, Map< String, ?> params) { HttpEntity< ?> requestEntity = new HttpEntity< > (headers); return exchange(url, HttpMethod.GET, requestEntity, responseType, params); }// ----------------------------------POST-------------------------------------------------------/** * POST请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @return */ public static < T> ResponseEntity< T> post(String url, Class< T> responseType) { return restTemplate.postForEntity(url, HttpEntity.EMPTY, responseType); }/** * POST请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, Object requestBody, Class< T> responseType) { return restTemplate.postForEntity(url, requestBody, responseType); }/** * POST请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, Object requestBody, Class< T> responseType, Object... params) { return restTemplate.postForEntity(url, requestBody, responseType, params); }/** * POST请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, Object requestBody, Class< T> responseType, Map< String, ?> params) { return restTemplate.postForEntity(url, requestBody, responseType, params); }/** * 带请求头的POST请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Object... params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return post(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的POST请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return post(url, requestEntity, responseType, params); }/** * 带请求头的POST请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return post(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的POST请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return post(url, requestEntity, responseType, params); }/** * 自定义请求头和请求体的POST请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Object... params) { return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, params); }/** * 自定义请求头和请求体的POST请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> post(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Map< String, ?> params) { return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, params); }// ----------------------------------PUT-------------------------------------------------------/** * PUT请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, Class< T> responseType, Object... params) { return put(url, HttpEntity.EMPTY, responseType, params); }/** * PUT请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, Object requestBody, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody); return put(url, requestEntity, responseType, params); }/** * PUT请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody); return put(url, requestEntity, responseType, params); }/** * 带请求头的PUT请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Object... params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return put(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的PUT请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return put(url, requestEntity, responseType, params); }/** * 带请求头的PUT请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return put(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的PUT请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return put(url, requestEntity, responseType, params); }/** * 自定义请求头和请求体的PUT请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Object... params) { return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, params); }/** * 自定义请求头和请求体的PUT请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> put(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Map< String, ?> params) { return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, params); }// ----------------------------------DELETE-------------------------------------------------------/** * DELETE请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Class< T> responseType, Object... params) { return delete(url, HttpEntity.EMPTY, responseType, params); }/** * DELETE请求调用方式 * * @param url请求URL * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Class< T> responseType, Map< String, ?> params) { return delete(url, HttpEntity.EMPTY, responseType, params); }/** * DELETE请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Object requestBody, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody); return delete(url, requestEntity, responseType, params); }/** * DELETE请求调用方式 * * @param url请求URL * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody); return delete(url, requestEntity, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Map< String, String> headers, Class< T> responseType, Object... params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpHeaders headers, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (headers); return delete(url, requestEntity, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Map< String, String> headers, Class< T> responseType, Map< String, ?> params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpHeaders headers, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (headers); return delete(url, requestEntity, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Object... params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Object... params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return delete(url, requestEntity, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, Map< String, String> headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, requestBody, responseType, params); }/** * 带请求头的DELETE请求调用方式 * * @param url请求URL * @param headers请求头参数 * @param requestBody请求参数体 * @param responseType 返回对象类型 * @param params URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpHeaders headers, Object requestBody, Class< T> responseType, Map< String, ?> params) { HttpEntity< Object> requestEntity = new HttpEntity< Object> (requestBody, headers); return delete(url, requestEntity, responseType, params); }/** * 自定义请求头和请求体的DELETE请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Object... params) { return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, params); }/** * 自定义请求头和请求体的DELETE请求调用方式 * * @param url请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> delete(String url, HttpEntity< ?> requestEntity, Class< T> responseType, Map< String, ?> params) { return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, params); }// ----------------------------------通用方法-------------------------------------------------------/** * 通用调用方式 * * @param url请求URL * @param method请求方法类型 * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> exchange(String url, HttpMethod method, HttpEntity< ?> requestEntity, Class< T> responseType, Object... params) { return restTemplate.exchange(url, method, requestEntity, responseType, params); }/** * 通用调用方式 * * @param url请求URL * @param method请求方法类型 * @param requestEntity 请求头和请求体封装对象 * @param responseType返回对象类型 * @param paramsURL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static < T> ResponseEntity< T> exchange(String url, HttpMethod method, HttpEntity< ?> requestEntity, Class< T> responseType, Map< String, ?> params) { return restTemplate.exchange(url, method, requestEntity, responseType, params); }/** * 获取RestTemplate实例对象,可自由调用其方法 * * @return RestTemplate实例对象 */ public static RestTemplate getRestTemplate() { return restTemplate; }}

测试代码
/** * RestTemplate 功能测试类 * * 需要先启动部署项目,再进行测试 */ public class RestTemplateUtilsTest { private String baseUrl = "http://localhost:80/trtd/dept3/"; @Test public void get11() { ResponseEntity< String> res = RestTemplateUtils.get(baseUrl + "demo11", String.class); System.out.println(res); }@Test public void get12() { ResponseEntity< String> res = RestTemplateUtils.get(baseUrl + "demo12?dname={dname}& loc={loc}", String.class, "sales", "NewYork"); System.out.println(res); }@Test public void post13() { Map< String, Object> map = new HashMap< > (); map.put("no", 33); map.put("name", "haha"); map.put("loc", "hahahahahaha"); ResponseEntity< String> res = RestTemplateUtils.get(baseUrl + "demo13?deptno={no}& dname={name}& loc={loc}", String.class, map); System.out.println(res); }@Test public void post21() { ResponseEntity< String> res = RestTemplateUtils.post(baseUrl + "demo21", String.class); System.out.println(res); }@Test public void post22() { MultiValueMap< String, String> multiValueMap = new LinkedMultiValueMap< > (); multiValueMap.set("dname", "sales"); multiValueMap.set("loc", "china"); ResponseEntity< String> res = RestTemplateUtils.post(baseUrl + "demo22", multiValueMap, String.class); System.out.println(res); }@Test public void post231() { MultiValueMap< String, String> multiValueMap = new LinkedMultiValueMap< > (); multiValueMap.set("dname", "sales"); multiValueMap.set("loc", "china"); ResponseEntity< String> res = RestTemplateUtils.post(baseUrl + "demo23?query={0}& deptno={1}", multiValueMap, String.class, "fdsfsdf", 55); System.out.println(res); }@Test public void post232() { MultiValueMap< String, String> multiValueMap = new LinkedMultiValueMap< > (); multiValueMap.set("dname", "sales"); multiValueMap.set("loc", "china"); Map< String, Object> map = new HashMap< > (); map.put("q", "haha"); map.put("no", 99); ResponseEntity< String> res = RestTemplateUtils.post(baseUrl + "demo23?query={q}& deptno={no}", multiValueMap, String.class, map); System.out.println(res); } }


    推荐阅读