SpringBoot使用RestTemplate远程调用其他服务接口
说明
这里记录一下我简单学习RestTemplate远程调用其他服务接口的过程。怕以后忘记怎么使用,先做个笔记。
远程服务接口代码
这里我新建了一个项目,结构很简单,用来模拟远程服务。不涉及数据库表查询。
项目结构如下
pom.xml文件代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web依赖,内嵌入tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok依赖,用来对象省略写set、get方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserParam请求对象代码
package com.project.test.dto;
import lombok.Data;
@Data
public class UserParam {
private String username;
private String password;
}
TestController代码
假设接口是以如下的http接口形式
package com.project.test.controller;
import com.project.test.dto.UserParam;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/test")
public class TestController {
@GetMapping(value = "/queryNonparametricGetRequestTest")
public Map<String, Object> queryNonparametricGetRequestTest(){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("name","张三");
map.put("city","上海");
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("name","李四");
map1.put("city","北京");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@GetMapping(value = "/queryParameterGetRequestTest")
public Map<String, Object> queryParameterGetRequestTest(@RequestParam(value = "username", required = false) String username,
@RequestParam(value = "password", required = false) String password){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("username",username);
map.put("password",password);
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("username","李四");
map1.put("password","test1234");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@GetMapping(value = "/queryObjectGetRequestTest")
public Map<String, Object> queryObjectGetRequestTest(@RequestBody UserParam userParam){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("username",userParam.getUsername());
map.put("password",userParam.getPassword());
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("username","李四");
map1.put("password","test1234");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@GetMapping(value = "/queryPlaceholderGetRequestTest/{id}")
public Map<String, Object> queryPlaceholderGetRequestTest(@PathVariable Long id){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("id",id);
data.add(map);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@PostMapping(value = "/queryNonparametricPostRequestTest")
public Map<String, Object> queryNonparametricPostRequestTest(){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("name","张三");
map.put("city","广州");
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("name","李四");
map1.put("city","深圳");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@PostMapping(value = "/queryParameterPostRequestTest")
public Map<String, Object> queryParameterPostRequestTest(@RequestParam(value = "username", required = false) String username,
@RequestParam(value = "password", required = false) String password){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("username",username);
map.put("password",password);
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("username","李四");
map1.put("password","test1234");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@PostMapping(value = "/queryObjectPostRequestTest")
public Map<String, Object> queryObjectPostRequestTest(@RequestBody UserParam userParam){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("username",userParam.getUsername());
map.put("password",userParam.getPassword());
data.add(map);
Map<String, Object> map1=new HashMap<>();
map1.put("username","李四");
map1.put("password","test1234");
data.add(map1);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
@PostMapping(value = "/queryPlaceholderPostRequestTest/{id}")
public Map<String, Object> queryPlaceholderPostRequestTest(@PathVariable Long id){
Map<String, Object> returnMap = new HashMap<>();
List<Map<String, Object>> data=new ArrayList<>();
Map<String, Object> map=new HashMap<>();
map.put("id",id);
data.add(map);
returnMap.put("code","200");
returnMap.put("message","查询成功!");
returnMap.put("result",data);
return returnMap;
}
}
application.yml代码
这里只设置了端口号为8081,怕与其他项目端口冲突
PostMan本地调用接口效果
这里在本地先用postman调用接口看一下返回的数据结构。 无参get请求queryNonparametricGetRequestTest接口方法返回结果如下: 有参get请求queryParameterGetRequestTest接口方法返回结果如下: 以json形式传参,有参get请求queryObjectGetRequestTest接口方法返回结果如下: 以占位符形式传参,有参get请求queryPlaceholderGetRequestTest接口方法返回结果如下: 无参post请求queryNonparametricPostRequestTest接口方法返回结果如下: 有参post请求queryParameterPostRequestTest接口方法返回结果如下: 以json形式传参,有参post请求queryObjectPostRequestTest接口方法返回结果如下: 以占位符形式传参,有参post请求queryPlaceholderPostRequestTest接口方法返回结果如下: 测试接口没有问题,然后我把这个服务打成jar包放到一台linux机器上面并使用nohup启动。
RestTemplate调用服务代码
这里我新建了一个项目,结构很简单,用来模拟调用远程服务。不涉及数据库表查询。
项目结构如下
pom.xml文件代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>rest-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-demo</name>
<description>rest-demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web依赖,内嵌入tomcat,RestTempLate使用该依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--当使用get请求需要携带 body中的参数的时候 需要重写Http 版本号必须是4.3以上版本-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<!--用来将string的json格式字符串转换成json对象-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<!--lombok依赖,用来对象省略写set、get方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserParam请求对象代码
package com.project.restdemo.dto;
import lombok.Data;
@Data
public class UserParam {
private String username;
private String password;
}
RestTemplateConfig配置代码
package com.project.restdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
return restTemplate;
}
public ClientHttpRequestFactory clientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory=new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(10000);
return factory;
}
}
application.yml配置代码
只配置了端口号,这样多个项目同时启动不会端口冲突
RestTestController代码
这里面每一个调用远程服务方法都写了怎么使用getForObject或者getForEntity或者postForObject或者postForEntity,之所以这么写是为了看下getForObject和getForEntity有什么区别。 这里一共写了17个方法,最后一个方法更偏向实际应用,前面16个方法参数值都是写死的。
package com.project.restdemo.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.restdemo.dto.UserParam;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/rest")
public class RestTestController {
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/getForObjectNonparametricTest")
public String getForObjectNonparametricTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryNonparametricGetRequestTest";
String result = restTemplate.getForObject(url, String.class);
System.out.println(result);
return result;
}
@GetMapping(value = "/getForEntityNonparametricTest")
public String getForEntityNonparametricTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryNonparametricGetRequestTest";
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = {"/getForObjectParameterGetRequestTest"})
public String getForObjectParameterGetRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryParameterGetRequestTest?username={username}&password={password}";
Map<String, Object> map = new HashMap<>();
map.put("username", "张三");
map.put("password", "test111");
String result = restTemplate.getForObject(url, String.class,map);
System.out.println(result);
return result;
}
@PostMapping(value = {"/getForEntityParameterGetRequestTest"})
public String getForEntityParameterGetRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryParameterGetRequestTest?username={username}&password={password}";
Map<String, Object> map = new HashMap<>();
map.put("username", "张三");
map.put("password", "test111");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class,map);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = {"/getForObjectObjectGetRequestTest"})
public String getForObjectObjectGetRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryObjectGetRequestTest";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
headers.add("Accept",MediaType.APPLICATION_JSON.toString());
headers.add("Accept-Charset","UTF-8");
UserParam userParam=new UserParam();
userParam.setUsername("张三");
userParam.setPassword("test111");
HttpEntity<?> request = new HttpEntity<>(userParam, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = {"/getForObjectPlaceholderGetRequestTest"})
public String getForObjectPlaceholderGetRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryPlaceholderGetRequestTest/{id}";
String result = restTemplate.getForObject(url, String.class,11);
System.out.println(result);
return result;
}
@PostMapping(value = {"/getForEntityPlaceholderGetRequestTest"})
public String getForEntityPlaceholderGetRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryPlaceholderGetRequestTest/{id}";
Map<String, Object> map = new HashMap<>();
map.put("id", 11);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class,map);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@GetMapping(value = "/getForObjectNonparametricPostTest")
public String getForObjectNonparametricPostTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryNonparametricPostRequestTest";
String result = restTemplate.postForObject(url, null,String.class);
System.out.println(result);
return result;
}
@GetMapping(value = "/getForEntityNonparametricPostTest")
public String getForEntityNonparametricPostTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryNonparametricPostRequestTest";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, null, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = "/getForObjectParameterPostRequestTest")
public String getForObjectParameterPostRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryParameterPostRequestTest";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("username", "张三");
map.add("password", "test111");
String result = restTemplate.postForObject(url, map,String.class);
System.out.println(result);
return result;
}
@PostMapping(value = "/getForEntityParameterPostRequestTest")
public String getForEntityParameterPostRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryParameterPostRequestTest";
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("username", "张三");
map.add("password", "test111");
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = "/getForObjectParameterPostRequestTest2")
public String getForObjectParameterPostRequestTest2(){
String url = "http://你的远程服务ip:服务端口号/test/queryParameterPostRequestTest";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("username", "张三");
map.add("password", "test111");
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
String result = restTemplate.postForObject(url, request,String.class);
System.out.println(result);
return result;
}
@PostMapping(value = "/getForObjectObjectPostRequestTest")
public String getForObjectObjectPostRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryObjectPostRequestTest";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
headers.add("Accept",MediaType.APPLICATION_JSON.toString());
headers.add("Accept-Charset","UTF-8");
UserParam userParam=new UserParam();
userParam.setUsername("张三");
userParam.setPassword("test111");
HttpEntity<UserParam> request = new HttpEntity<>(userParam, headers);
String result = restTemplate.postForObject(url, request,String.class);
System.out.println(result);
return result;
}
@PostMapping(value = "/getForEntityObjectPostRequestTest")
public String getForEntityObjectPostRequestTest(){
String url = "http://你的远程服务ip:服务端口号/test/queryObjectPostRequestTest";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
headers.add("Accept",MediaType.APPLICATION_JSON.toString());
headers.add("Accept-Charset","UTF-8");
UserParam userParam=new UserParam();
userParam.setUsername("张三");
userParam.setPassword("test111");
HttpEntity<UserParam> request = new HttpEntity<>(userParam, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = "/getForObjectPlaceholderPostRequestTest")
public String getForObjectPlaceholderPostRequestTest(@RequestParam(value = "id", required = false) Integer id){
String url = "http://你的远程服务ip:服务端口号/test/queryPlaceholderPostRequestTest/"+id;
String result = restTemplate.postForObject(url, null,String.class);
System.out.println(result);
return result;
}
@PostMapping(value = "/getForEntityPlaceholderPostRequestTest")
public String getForEntityPlaceholderPostRequestTest(@RequestParam(value = "id", required = false) Integer id){
String url = "http://你的远程服务ip:服务端口号/test/queryPlaceholderPostRequestTest/"+id;
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, null, String.class);
String body = responseEntity.getBody();
System.out.println("HTTP 响应body:" + body);
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders httpHeaders = responseEntity.getHeaders();
System.out.println("HTTP 响应状态:" + statusCode);
System.out.println("HTTP 响应状态码:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + httpHeaders);
return body;
}
@PostMapping(value = "/test")
public Object test(@RequestBody UserParam userParam){
String url = "http://你的远程服务ip:服务端口号/test/queryObjectPostRequestTest";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
headers.add("Accept",MediaType.APPLICATION_JSON.toString());
headers.add("Accept-Charset","UTF-8");
HttpEntity<UserParam> request = new HttpEntity<>(userParam, headers);
String result = restTemplate.postForObject(url, request,String.class);
System.out.println(result);
JSONObject jsonObject = JSON.parseObject(result);
System.out.println(jsonObject);
return jsonObject;
}
}
测试结果如下:
调用接口getForObjectNonparametricTest如下: 调用接口getForEntityNonparametricTest如下: 调用接口getForObjectParameterGetRequestTest如下: 调用接口getForEntityParameterGetRequestTest如下: 调用接口getForObjectObjectGetRequestTest如下: 调用接口getForObjectPlaceholderGetRequestTest如下: 调用接口getForEntityPlaceholderGetRequestTest如下: 调用接口getForObjectNonparametricPostTest如下: 调用接口getForEntityNonparametricPostTest如下: 调用接口getForObjectParameterPostRequestTest如下: 调用接口getForEntityParameterPostRequestTest如下: 调用接口getForObjectParameterPostRequestTest2如下: 这个接口只是在代码里面在请求头里面加了请求数据格式,请求的数据和getForObjectParameterPostRequestTest是一样的。 调用接口getForObjectObjectPostRequestTest如下: 调用接口getForEntityObjectPostRequestTest如下: 调用接口getForObjectPlaceholderPostRequestTest如下: 调用接口getForEntityPlaceholderPostRequestTest如下: 调用接口test如下: 好了,就先写这些通用方法的get和post远程调用方法。这里只写了在开发过程中可能遇到的请求形式。等以后有新的在补充一下。
|