RestTemplate通过http的get或post访问本机接口
程序员文章站
2022-06-26 12:01:44
...
RestTemplate通过http的get或post访问本机接口
get请求
我在做junit的单元测试测试本机的接口时通过http来访问后台接口,下面讲一下将消息体ApiHeader封装在头部,还有类似URL为http://localhost:8080/api/User/Login?user=…&password=…这种形式的访问方法:
首先创建一个HttpClients类
@Service
public class HttpClients {
HttpHeaders headers;
public JSONObject sendGet(String url, JSONObject headerData,JSONObject body){
//请求头
headers = new HttpHeaders();
//设置请求内容为JSON类型
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
if(headerData != null){
// header填充
headers.add("ApiHeader", headerData.toString());
}
RestTemplate restTemplate = new RestTemplate();
//创建请求实体对象
if(body!= null){
//获取JSONObject 中的key的值用迭代器遍历
Iterator<String> it = body.keys();
url = url+"?";
while(it.hasNext()){
// 获得key
String key = it.next();
String value = body.getString(key);
url = url + key + "="+value;
if(it.hasNext()){
url=url+"&";
}
}
System.out.println(url);
}
HttpEntity<JSONObject> request = new HttpEntity<JSONObject>(body,headers);//将参数和header组成一个请求
//我尝试用这样的方法访问但是不能成功
//ResponseEntity<JSONObject> response = restTemplate.getForEntity(url, JSONObject.class,request);
ResponseEntity<JSONObject> response =restTemplate.exchange(url, HttpMethod.GET, request, JSONObject.class);
JSONObject requestData = response.getBody();
return requestData;
}
}
POST请求
但是在新增用户时字段比较多时,往往会采用传递请求体(Body)的方式,对请求进行一定的设置进行POST请求。下面是我的代码:
public JSONObject sendPost(String url, JSONObject headerData,JSONObject bodydata){
//请求头
headers = new HttpHeaders();
//设置请求内容为JSON类型
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
if(headerData != null){
// header填充
headers.add("ApiHeader", headerData.toString());
}
//创建请求实体对象
HttpEntity<JSONObject> request = new HttpEntity<>(bodydata,headers);
RestTemplate restTemplate = new RestTemplate();
System.out.println(request);
ResponseEntity<JSONObject> requestEntity= restTemplate.postForEntity(url,request,JSONObject.class);
JSONObject requestData = requestEntity.getBody();
/* //获取响应头
HttpHeaders respHeaders = requestEntity.getHeaders();
//获取响应属性
respHeaders.get("success");
//响应的HTTP状态码
int status = requestEntity.getStatusCodeValue();*/
return requestData;
}
测试
接下来我用junit的单元测试的方法来测试一下:
这是单元测试类的代码:
class HttpPostetmfTest {
public static final String URL = "http://localhost:8080";
HttpData httpData;
HttpClients httpClients;
@BeforeEach
void setUp() {
httpData= new HttpData ();
httpClients = new HttpClients();
}
@Test
void RefreshToken() {
JSONObject postLogin = httpClients.sendPost(URL+"/api/User/Login",null,
httpData.api_User_Login());
JSONObject refreshToken = httpClients.sendPost(URL+"/api/User/RefreshToken",
httpData.api_Config_CompanyConfigGet(postLogin),
httpData.api_User_RefreshToken(postLogin));
System.out.println(refreshToken);
assertNotNull(refreshToken);
}
}
下面是我想要传送的json数据:
public class HttpData
{
public JSONObject api_User_Login(){
JSONObject request = new JSONObject();
request.put("UserName","...");
request.put("PassWord","...");
return request;
}
public JSONObject api_User_RefreshToken(JSONObject postresults){
JSONObject jsonObject = JSONObject.fromObject(postresults);
JSONObject request = new JSONObject();
request.put("LanguageType","utf8");
//从postresults中取出需要的数据封装刷新需要的数据
JSONArray data = jsonObject.getJSONObject("Result").getJSONArray("Items");
JSONObject row = null;
for(int i=0; i<data.size(); i++){
row = data.getJSONObject(i);
request.put("Id",row.get("Id"));
break;
}
return request;
}
}
get的测试也和post请求相差不大这里就不进行过多的演示,就是根据接口的需要调用sendGet和sendPost。
pom.xml需要引入的依赖
//junit5的依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
//JSON依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>