Springboot配置Okhtth3

Posted 一zhi程序猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot配置Okhtth3相关的知识,希望对你有一定的参考价值。

导入配置:

 <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>

  写的几个类

 

OkHttpRequestBuilder类的代码

  

package com.democontroller.Okhtth3;


import okhttp3.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class OkHttpRequestBuilder <T extends OkHttpRequestBuilder>{
    protected String url;
    protected Map<String, String> stringMap;
    protected Map<String, String> headerMap;
    protected Map<String,String> paramMap;

    public T url(String url) {
        this.url = url;
        return (T) this;
    }

    public T addParam(String key, String val) {
        if (this.paramMap == null) {
            paramMap = new LinkedHashMap<>();
        }
        paramMap.put(key, val);
        return (T) this;
    }

    public T addHeader(String key, String val) {
        if (this.headerMap == null) {
            headerMap = new LinkedHashMap<>();
        }
        headerMap.put(key, val);
        return (T) this;
    }

    public T addUrlString(String key, String val) {
        if (this.stringMap == null) {
            stringMap = new LinkedHashMap<>();
        }
        stringMap.put(key, val);
        return (T) this;
    }

    protected Request.Builder request() {
        Request.Builder request = new Request.Builder();
        String str = "";
        if(stringMap!=null) {
            Iterator<Map.Entry<String, String>> iterator = stringMap.entrySet().iterator();
            while (iterator.hasNext()){
                try {
                    Map.Entry<String, String> entry = iterator.next();
                    String key = URLEncoder.encode(entry.getKey(), "utf-8");
                    String value = URLEncoder.encode(entry.getValue(), "utf-8");
                    str += key+"="+value+"&";
                }catch (UnsupportedEncodingException e){
                    e.printStackTrace();
                }
            }
        }
        if (StringUtils.endsWith(str, "&")) {
            str = StringUtils.removeEnd(str, "&");
        }

        if(headerMap!=null){
            Iterator<Map.Entry<String, String>> iterator = headerMap.entrySet().iterator();
            while (iterator.hasNext()){
                    Map.Entry<String, String> entry = iterator.next();
                    request.addHeader(entry.getKey(),entry.getValue());
            }
        }
        return request.url("".equals(str)?url:url+"?"+str);
    }

    protected abstract Call createCall();

    protected RequestBody createBody(){
        RequestBody requestBody ;
        FormBody.Builder formBody = new FormBody.Builder();
        if(paramMap!=null){
            Iterator<Map.Entry<String,String>> iterator = paramMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String,String> entry = iterator.next();
                formBody.add(entry.getKey(),entry.getValue());
            }
        }
        requestBody = formBody.build();
        return requestBody;
    }

    public String execute(){
        Call call = createCall();
        try {
            Response response = call.execute();
            return  response.body().string();
        }catch (IOException e){
            e.getMessage();
        }
        return "";
    }
}

 

GetBuilder类
package com.democontroller.Okhtth3;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> {
    @Override
    protected Call createCall() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder request = request();
        return okHttpClient.newCall(request.get().build());
    }

}

  

PostBuilder类
package com.democontroller.Okhtth3;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class PostBuilder extends OkHttpRequestBuilder<PostBuilder> {
    @Override
    protected Call createCall() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder request = request();
        RequestBody requestBody = createBody();
        return okHttpClient.newCall(request.post(requestBody).build());
    }
}

  

OkHttpUtils类
package com.democontroller.Okhtth3;

public class OkHttpUtils {
    public static GetBuilder get() {
        return new GetBuilder();
    }

    public static PostBuilder post() {
        return new PostBuilder();
    }

    public static void main(String[] args) {
        System.out.println(get().url("http://www.baidu.com")
                .addUrlString("aa","参数1")
                .addUrlString("bb","参数2")
                .execute());
    }
}

 

以上是关于Springboot配置Okhtth3的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段

SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段

SpringBoot 部署 Jar 文件,瘦身优化指南 !

项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde(代码片段

Spring boot:thymeleaf 没有正确渲染片段

VSCode自定义代码片段11——vue路由的配置