我们后端在工作中,可能接触最多的无非是接口请求了,之前用的最多的是httpclient,后来通过一系列的对比可以发现,okhttp比httpclient更加优秀。因此,写了OkHttp-FastJson,一个让网络请求更简单的框架。
这个是项目地址:github地址
<dependency>
<groupId>github.hjggroupId>
<artifactId>okhttp-fastjsonartifactId>
<version>1.0.0version>
dependency>
/**
* 自定义请求
*
* @param request
* @param responseType
* @param
* @return
* @throws InterfaceException
*/
public T requestForObject(Request request, Class responseType) throws InterfaceException {
ObjectExtractor extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
return handleResponse(null, request, extractor);
}
/**
* GET
*
* @param url
* @param responseType
* @param uriVariables
* @param
* @return
* @throws InterfaceException
*/
public T getForObject(String url, Class responseType, Object... uriVariables) throws InterfaceException {
Request request = new Request.Builder().url(StringUriTemplateUtil.expand(url, uriVariables)).build();
ObjectExtractor extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
return handleResponse(null, request, extractor);
}
/**
* POST
* @param url
* @param responseType
* @param content
* @param mediaType
* @param
* @return
* @throws InterfaceException
*/
public T postForObject(String url, Class responseType, String content, MediaType mediaType)
throws InterfaceException {
Request request = new Request.Builder().url(url).post(RequestBody.create(mediaType, content)).build();
ObjectExtractor extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
return handleResponse(null, request, extractor);
}
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class ProductVo {
private int id ;
private String name ;
private double price ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.JSON_STYLE);
return ReflectionToStringBuilder.toString(this);
}
}
import github.hjg.InterfaceException;
import github.hjg.JsonInterface;
import github.hjg.OkHttpInterface;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
//模拟服务端
@GetMapping("/getService")
public String getService() {
return "{\"id\":1,\"name\":\"test\",\"price\":23.01}";
}
//模拟客户端
@GetMapping("/get")
public String getTest() throws InterfaceException {
OkHttpInterface okhttpInterface = new JsonInterface();
ProductVo product = okhttpInterface.getForObject("http://localhost:8080/test/getService", ProductVo.class,"");
return product.toString();
}
{
id: 1,
name: "test",
price: 23.01
}
OkHttpInterface okHttpInterface = new JsonInterface(
new OkHttpConfig.Builder()
.connectTimeout(3000) //配置连接超时参数,默认10000
.readTimeout(3000) //读超时参数,默认10000
.writeTimeout(3000) //写超时参数,默认10000
//以及对这些连接的的连接新池参数配置
.maxIdleConnections(5) //最大空闲连接数,默认5个
.keepAliveDuration(300000).build()); //,最大存活时间,默认300000