okhttp,让你的网络请求变得更加简单。

我们后端在工作中,可能接触最多的无非是接口请求了,之前用的最多的是httpclient,后来通过一系列的对比可以发现,okhttp比httpclient更加优秀。因此,写了OkHttp-FastJson,一个让网络请求更简单的框架。

这个是项目地址:github地址

OkHttp-FastJson是什么?

  • 网络框架使用最热门的OkHttp3,比HttpClient性能更强,引用api更简单!
  • 自动帮你解析请求响应的JSON数据,让你专注于处理业务逻辑。JSON解析用阿里的fastJson,速度比gson和fackson更快,引用api更简单!

快速入门

  • 克隆本项目,并mvn install。
  • 在pom.xml引入okhttp-fastjson。
    <dependency>
        <groupId>github.hjg</groupId>
    <artifactId>okhttp-fastjson</artifactId>
    <version>1.0.0</version>
</dependency>
  • okhttp-fastjson提供了三种请求方式
     /**
     * 自定义请求
     * 
     * @param request
     * @param responseType
     * @param <T>
     * @return
     * @throws InterfaceException
     */
    public <T> T requestForObject(Request request, Class<T> responseType) throws InterfaceException {
        ObjectExtractor<T> extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
        return handleResponse(null, request, extractor);
    }
    /**
     * GET
     * 
     * @param url
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     * @throws InterfaceException
     */
    public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws InterfaceException {
        Request request = new Request.Builder().url(StringUriTemplateUtil.expand(url, uriVariables)).build();
        ObjectExtractor<T> extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
        return handleResponse(null, request, extractor);
    }
     /**
     * POST
     * @param url
     * @param responseType
     * @param content
     * @param mediaType
     * @param <T>
     * @return
     * @throws InterfaceException
     */
    public <T> T postForObject(String url, Class<T> responseType, String content, MediaType mediaType)
            throws InterfaceException {
        Request request = new Request.Builder().url(url).post(RequestBody.create(mediaType, content)).build();
        ObjectExtractor<T> extractor = new ObjectExtractor<>(fastJsonHelper(), responseType);
        return handleResponse(null, request, extractor);
    }
  • 这是get请求的例子。
  • 新建web项目,新建JavaBean:ProductVo类和测试类TestController类,然后“../test/getService”模拟Web接口服务端,“../test/get”模拟客户端去调用服务端

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 okHttpInterface = new JsonInterface(
        new OkHttpConfig.Builder()
            .connectTimeout(3000) //配置连接超时参数,默认10000
            .readTimeout(3000) //读超时参数,默认10000
            .writeTimeout(3000) //写超时参数,默认10000
            //以及对这些连接的的连接新池参数配置
            .maxIdleConnections(5) //最大空闲连接数,默认5个
            .keepAliveDuration(300000).build()); //,最大存活时间,默认300000

猜你喜欢

转载自blog.csdn.net/h295928126/article/details/78565815
今日推荐