在开发过程中,用到在模拟api的简单服务器。
Moco已经在github上开源,可点击连接:https://github.com/dreamhead/moco
下载包在话也可以直接在Central Repository: com/github/dreamhead/moco-runner
运行命令
java -jar moco-runner-1.3.0-standalone.jar http -p 80 -c user.json
user.json 内容为:
[
{
"description":"http://127.0.0.1/demo",
"request":{
"uri":"/demo"
},
"response":
{
"text":"Hello WORLD"
}
},
{ "description":"http://127.0.0.1/demo?name=xiaohong&age=age",
"request":{
"uri":"/demo/user",
"method":"get",
"queries":{
"name":"xiaohong",
"age":"18"
}
},
"redirectTo":"/demo/search"
},
{
"request":{
"uri":"/demo/search",
"method" : "get"
},
"response":{
"headers":{"Content-Type":"text/html;charset=utf-8"},
"status": 200,
"json": {
"code": 1,
"msg": "查询成功",
"data": {
"userid": 1001,
"username": "tomcat"
}
},
"cookies" :{
"login" : "true"
}
}
},
{
"request":{
"uri":"/demo/search",
"method" : "post",
"json" :{
"name" : "jack"
}
},
"response":{
"headers":{"Content-Type":"text/html;charset=utf-8"},
"status": 200,
"json": {
"code": 1,
"msg": "查询成功",
"data": {
"userid": 1001,
"username": "tomcat"
}
},
"cookies" :{
"login" : "true"
}
}
},
{
"request":{
"uri":"/demo/user",
"headers":{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},
"method" : "post",
"forms" : {
"userid": "1001",
"username": "tomcat"
}
},
"response":
{
"text":"Hello WORLD"
}
}
]
Wisdom rest-client 一款自动化测试REST API的工具,它可以自动化测试REST API并生成精美的测试报告,同时基于测试过的历史数据,可以生成精美的REST API文档。
https://github.com/Wisdom-Projects/
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
HttpComponentsClientRestfulHttpRequestFactory(为get能传递json数据)
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import java.net.URI;
public class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestWithEntity(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}
/**
* 定义HttpGetRequestWithEntity实现HttpEntityEnclosingRequestBase抽象类,以支持GET请求携带body数据
*/
private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetRequestWithEntity(final URI uri) {
super.setURI(uri);
}
@Override
public String getMethod() {
return HttpMethod.GET.name();
}
}
}
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson2.JSONObject;
public class TestWeb {
public static RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
return restTemplate;
}
private static SimpleClientHttpRequestFactory getClientHttpRequestFactory() {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
clientHttpRequestFactory.setConnectTimeout(60000);
clientHttpRequestFactory.setReadTimeout(60000);
return clientHttpRequestFactory;
}
public static void main(String[] args) {
String url = "http://localhost:6060/hxms/login";
RestTemplate restTemplate = restTemplate() ;
JSONObject object=new JSONObject();
object.put("username","hxms");
object.put("password","123456");
String jsonData = object.toString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(jsonData, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
// post
// ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
HttpStatus statusCode = responseEntity.getStatusCode();
String body = responseEntity.getBody();
System.out.println(statusCode);
System.out.println(body);
}
}