使用java编写一个简单的实现restful请求的的工具类
1、加入以下pom依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2、工具类代码
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
/**
* @Author: zxb
* @Date : 2020/10/13 9:35 下午
*/
public class HttpClientUtil {
@SuppressWarnings("deprecation")
/**
* 模拟post请求
*/
public static String postRest(String url, JSONObject json) throws IOException {
HttpClient httpClient = new HttpClient();
//创建一个post请求
PostMethod postMethod = new PostMethod(url);
//设置传送消息的格式
// 设置为JSON格式调用
postMethod.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
postMethod.setRequestBody(json.toString());
int code = httpClient.executeMethod(postMethod);
System.out.println("消息码为:" + code);
String result = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
}
/**
* 模拟get请求
*
* @param url
* @return
* @throws IOException
*/
public static String getRest(String url) throws IOException {
HttpClient httpClient = new HttpClient();
// 创建一个get请求
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
int code = httpClient.executeMethod(getMethod);
System.out.println("消息码为:" + code);
String result = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return result;
}
public static void delRest(String url) throws IOException {
HttpClient httpClient = new HttpClient();
DeleteMethod deleteMethod = new DeleteMethod(url);
deleteMethod.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
int code = httpClient.executeMethod(deleteMethod);
System.out.println("消息码为:" + code);
String result = deleteMethod.getResponseBodyAsString();
System.out.println("Result:" + result);
deleteMethod.releaseConnection();
}
}
3、单元测试
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import java.io.IOException;
/**
* @Author: zxb
* @Date : 2020/10/13 9:54 下午
*/
public class HttpClientUtilTest {
@Test
public void testPostRest() throws IOException {
String url = "http://localhost:8080/jpa/user/";
JSONObject json = new JSONObject();
json.put("name", "eric");
json.put("email", "eric@163.com");
System.out.println(json);
String result = HttpClientUtil.postRest(url, json);
JSONObject jsonObject = JSON.parseObject(result);
System.out.println(JSON.toJSONString(jsonObject, true));
}
@Test
public void testGetRest() throws IOException {
String url = "http://localhost:8080/jpa/users";
String result = HttpClientUtil.getRest(url);
System.out.println(result);
}
}
测试运行截图:
数据库表内容:
请求的服务api url