正在显示
9 个修改的文件
包含
812 行增加
和
0 行删除
package-lock.json
0 → 100644
pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | + | ||
3 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
5 | + <modelVersion>4.0.0</modelVersion> | ||
6 | + | ||
7 | + <groupId>com.sunyo.wlpt.message.bus.sdk</groupId> | ||
8 | + <artifactId>message_bus_sdk</artifactId> | ||
9 | + <version>1.0.0</version> | ||
10 | + | ||
11 | + <name>message_bus_sdk</name> | ||
12 | + <!-- FIXME change it to the project's website --> | ||
13 | + <url>http://www.example.com</url> | ||
14 | + | ||
15 | + <properties> | ||
16 | + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
17 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
18 | + <maven.compiler.source>1.8</maven.compiler.source> | ||
19 | + <maven.compiler.target>1.8</maven.compiler.target> | ||
20 | + </properties> | ||
21 | + | ||
22 | + <dependencies> | ||
23 | + | ||
24 | + <dependency> | ||
25 | + <groupId>com.squareup.okhttp3</groupId> | ||
26 | + <artifactId>okhttp</artifactId> | ||
27 | + <version>4.8.1</version> | ||
28 | + </dependency> | ||
29 | + | ||
30 | + <dependency> | ||
31 | + <groupId>org.apache.httpcomponents</groupId> | ||
32 | + <artifactId>httpclient</artifactId> | ||
33 | + <version>4.5.12</version> | ||
34 | + </dependency> | ||
35 | + | ||
36 | + <dependency> | ||
37 | + <groupId>org.apache.commons</groupId> | ||
38 | + <artifactId>commons-lang3</artifactId> | ||
39 | + <version>3.11</version> | ||
40 | + </dependency> | ||
41 | + | ||
42 | + <dependency> | ||
43 | + <groupId>com.google.code.gson</groupId> | ||
44 | + <artifactId>gson</artifactId> | ||
45 | + <version>2.8.6</version> | ||
46 | + </dependency> | ||
47 | + | ||
48 | + | ||
49 | + <dependency> | ||
50 | + <groupId>log4j</groupId> | ||
51 | + <artifactId>log4j</artifactId> | ||
52 | + <version>1.2.17</version> | ||
53 | + </dependency> | ||
54 | + | ||
55 | + <dependency> | ||
56 | + <groupId>org.slf4j</groupId> | ||
57 | + <artifactId>slf4j-nop</artifactId> | ||
58 | + <version>1.7.30</version> | ||
59 | + </dependency> | ||
60 | + | ||
61 | + <dependency> | ||
62 | + <groupId>org.slf4j</groupId> | ||
63 | + <artifactId>slf4j-simple</artifactId> | ||
64 | + <version>1.7.30</version> | ||
65 | + <scope>test</scope> | ||
66 | + </dependency> | ||
67 | + | ||
68 | + <dependency> | ||
69 | + <groupId>org.slf4j</groupId> | ||
70 | + <artifactId>slf4j-log4j12</artifactId> | ||
71 | + <version>1.7.30</version> | ||
72 | + <scope>test</scope> | ||
73 | + </dependency> | ||
74 | + | ||
75 | + <dependency> | ||
76 | + <groupId>org.slf4j</groupId> | ||
77 | + <artifactId>slf4j-api</artifactId> | ||
78 | + <version>1.7.30</version> | ||
79 | + </dependency> | ||
80 | + | ||
81 | + | ||
82 | + | ||
83 | + </dependencies> | ||
84 | + | ||
85 | + <build> | ||
86 | + <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
87 | + <plugins> | ||
88 | + <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> | ||
89 | + <plugin> | ||
90 | + <artifactId>maven-clean-plugin</artifactId> | ||
91 | + <version>3.1.0</version> | ||
92 | + </plugin> | ||
93 | + <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
94 | + <plugin> | ||
95 | + <artifactId>maven-resources-plugin</artifactId> | ||
96 | + <version>3.0.2</version> | ||
97 | + </plugin> | ||
98 | + <plugin> | ||
99 | + <artifactId>maven-compiler-plugin</artifactId> | ||
100 | + <version>3.8.0</version> | ||
101 | + </plugin> | ||
102 | + <plugin> | ||
103 | + <artifactId>maven-surefire-plugin</artifactId> | ||
104 | + <version>2.22.1</version> | ||
105 | + </plugin> | ||
106 | + <plugin> | ||
107 | + <artifactId>maven-jar-plugin</artifactId> | ||
108 | + <version>3.0.2</version> | ||
109 | + </plugin> | ||
110 | + <plugin> | ||
111 | + <artifactId>maven-install-plugin</artifactId> | ||
112 | + <version>2.5.2</version> | ||
113 | + </plugin> | ||
114 | + <plugin> | ||
115 | + <artifactId>maven-deploy-plugin</artifactId> | ||
116 | + <version>2.8.2</version> | ||
117 | + </plugin> | ||
118 | + <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> | ||
119 | + <plugin> | ||
120 | + <artifactId>maven-site-plugin</artifactId> | ||
121 | + <version>3.7.1</version> | ||
122 | + </plugin> | ||
123 | + <!-- <plugin>--> | ||
124 | + <!-- <artifactId>maven-project-info-reports-plugin</artifactId>--> | ||
125 | + <!-- <version>3.0.0</version>--> | ||
126 | + <!-- </plugin>--> | ||
127 | + </plugins> | ||
128 | + </pluginManagement> | ||
129 | + </build> | ||
130 | + </project> |
1 | +package com.sunyo.wlpt.message.bus.sdk; | ||
2 | + | ||
3 | +import com.sunyo.wlpt.message.bus.sdk.request.MessageRequest; | ||
4 | +import com.sunyo.wlpt.message.bus.sdk.response.BusResult; | ||
5 | + | ||
6 | +import java.io.IOException; | ||
7 | + | ||
8 | +/** | ||
9 | + * Hello world! | ||
10 | + */ | ||
11 | +public class App { | ||
12 | + public static void main(String[] args) | ||
13 | + { | ||
14 | + MessageRequest request = new MessageRequest(); | ||
15 | + BusResult result = request.getMessageOne("zicheng", "S_zicheng", | ||
16 | + "V_zicheng", "V_zicheng_zicheng_R"); | ||
17 | + System.out.println(result); | ||
18 | + } | ||
19 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.request; | ||
2 | + | ||
3 | +import com.sunyo.wlpt.message.bus.sdk.response.ResultJson; | ||
4 | +import com.sunyo.wlpt.message.bus.sdk.utils.GsonUtil; | ||
5 | +import com.sunyo.wlpt.message.bus.sdk.utils.OkHttpUtils; | ||
6 | +import okhttp3.MediaType; | ||
7 | + | ||
8 | + | ||
9 | +import java.io.IOException; | ||
10 | +import java.util.HashMap; | ||
11 | + | ||
12 | +/** | ||
13 | + * @author 子诚 | ||
14 | + * Description: | ||
15 | + * 时间:2020/9/3 15:18 | ||
16 | + */ | ||
17 | +public class BusMessage { | ||
18 | + | ||
19 | + private static final String URL_CONSUMER_ONE = "http://192.168.1.33:9032/bus/rabbit/consumer_one"; | ||
20 | + private static final String URL_CONSUMER_MORE = "http://192.168.1.33:9032/bus/rabbit/consumer_more"; | ||
21 | + | ||
22 | + | ||
23 | + private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); | ||
24 | + | ||
25 | + /** | ||
26 | + * 指定队列,消费一条消息 | ||
27 | + * | ||
28 | + * @param RUSR 接收者 | ||
29 | + * @param SERV 服务器名称 | ||
30 | + * @param VSHT 虚拟主机名称 | ||
31 | + * @param RCVR 队列名称 | ||
32 | + * @return | ||
33 | + * @throws IOException | ||
34 | + */ | ||
35 | + public static ResultJson consumerOne(String RUSR, String SERV, String VSHT, String RCVR) | ||
36 | + { | ||
37 | + HashMap<String, String> params = new HashMap<>(); | ||
38 | + params.put("RUSR", RUSR); | ||
39 | + params.put("SERV", SERV); | ||
40 | + params.put("VSHT", VSHT); | ||
41 | + params.put("RCVR", RCVR); | ||
42 | + | ||
43 | + OkHttpUtils okHttp = new OkHttpUtils(); | ||
44 | + String json = okHttp.doGet(URL_CONSUMER_ONE, params); | ||
45 | + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class); | ||
46 | + return result; | ||
47 | + } | ||
48 | + | ||
49 | + | ||
50 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.request; | ||
2 | + | ||
3 | +import com.sunyo.wlpt.message.bus.sdk.response.BusResult; | ||
4 | +import com.sunyo.wlpt.message.bus.sdk.response.ResultJson; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author 子诚 | ||
8 | + * Description: | ||
9 | + * 时间:2020/9/3 18:04 | ||
10 | + */ | ||
11 | +public class MessageRequest { | ||
12 | + | ||
13 | + private static final String SUCCESS_ONE_CODE = "20200"; | ||
14 | + | ||
15 | + /** | ||
16 | + * 接收消息,一条 | ||
17 | + * | ||
18 | + * @param RUSR 接收人名称 | ||
19 | + * @param SERV 服务器名称 | ||
20 | + * @param VSHT 虚拟主机名称 | ||
21 | + * @param RCVR 接收队列名称 | ||
22 | + * @return | ||
23 | + */ | ||
24 | + public BusResult getMessageOne(String RUSR, String SERV, String VSHT, String RCVR) | ||
25 | + { | ||
26 | + ResultJson resultJson = BusMessage.consumerOne(RUSR, SERV, VSHT, RCVR); | ||
27 | + if (resultJson == null) { | ||
28 | + | ||
29 | + } | ||
30 | + String code = resultJson.getCode(); | ||
31 | + String msg = resultJson.getMsg(); | ||
32 | + if (!SUCCESS_ONE_CODE.equals(resultJson.getCode())) { | ||
33 | + String resultMessage = "错误代码:" + code + ";错误信息:" + msg; | ||
34 | + BusResult errorResult = new BusResult(false, code, resultMessage); | ||
35 | + return errorResult; | ||
36 | + } | ||
37 | + String data = (String) resultJson.getData(); | ||
38 | + BusResult result = new BusResult(true, code, msg, data); | ||
39 | + return result; | ||
40 | + } | ||
41 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.response; | ||
2 | + | ||
3 | +import java.util.List; | ||
4 | + | ||
5 | +/** | ||
6 | + * @author 子诚 | ||
7 | + * Description:返回结果类 | ||
8 | + * 时间:2020/9/3 18:49 | ||
9 | + */ | ||
10 | +public class BusResult { | ||
11 | + | ||
12 | + /** | ||
13 | + * 是否成功? | ||
14 | + * true,成功 | ||
15 | + * false,失败 | ||
16 | + */ | ||
17 | + private Boolean isOk; | ||
18 | + | ||
19 | + /** | ||
20 | + * 状态代码 | ||
21 | + */ | ||
22 | + private String code; | ||
23 | + | ||
24 | + /** | ||
25 | + * 对应状态信息 | ||
26 | + */ | ||
27 | + private String message; | ||
28 | + | ||
29 | + /** | ||
30 | + * 接收一条消息的存放属性 | ||
31 | + */ | ||
32 | + private String data; | ||
33 | + | ||
34 | + /** | ||
35 | + * 接收多条消息的存放属性 | ||
36 | + */ | ||
37 | + private List<String> dataList; | ||
38 | + | ||
39 | + public BusResult(Boolean isOk, String code, String message) | ||
40 | + { | ||
41 | + this.isOk = isOk; | ||
42 | + this.code = code; | ||
43 | + this.message = message; | ||
44 | + } | ||
45 | + | ||
46 | + public BusResult(Boolean isOk, String code, String message, String data) | ||
47 | + { | ||
48 | + this.isOk = isOk; | ||
49 | + this.code = code; | ||
50 | + this.message = message; | ||
51 | + this.data = data; | ||
52 | + } | ||
53 | + | ||
54 | + public BusResult(Boolean isOk, String code, String message, List<String> dataList) | ||
55 | + { | ||
56 | + this.isOk = isOk; | ||
57 | + this.code = code; | ||
58 | + this.message = message; | ||
59 | + this.dataList = dataList; | ||
60 | + } | ||
61 | + | ||
62 | + public Boolean getOk() | ||
63 | + { | ||
64 | + return isOk; | ||
65 | + } | ||
66 | + | ||
67 | + public void setOk(Boolean ok) | ||
68 | + { | ||
69 | + isOk = ok; | ||
70 | + } | ||
71 | + | ||
72 | + public String getCode() | ||
73 | + { | ||
74 | + return code; | ||
75 | + } | ||
76 | + | ||
77 | + public void setCode(String code) | ||
78 | + { | ||
79 | + this.code = code; | ||
80 | + } | ||
81 | + | ||
82 | + public String getMessage() | ||
83 | + { | ||
84 | + return message; | ||
85 | + } | ||
86 | + | ||
87 | + public void setMessage(String message) | ||
88 | + { | ||
89 | + this.message = message; | ||
90 | + } | ||
91 | + | ||
92 | + public String getData() | ||
93 | + { | ||
94 | + return data; | ||
95 | + } | ||
96 | + | ||
97 | + public void setData(String data) | ||
98 | + { | ||
99 | + this.data = data; | ||
100 | + } | ||
101 | + | ||
102 | + public List<String> getDataList() | ||
103 | + { | ||
104 | + return dataList; | ||
105 | + } | ||
106 | + | ||
107 | + public void setDataList(List<String> dataList) | ||
108 | + { | ||
109 | + this.dataList = dataList; | ||
110 | + } | ||
111 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.response; | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | +import java.io.Serializable; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author 子诚 | ||
10 | + * Description:返回结果封装类 | ||
11 | + * 时间:2020/7/01 10:06 | ||
12 | + */ | ||
13 | +public class ResultJson<T> implements Serializable { | ||
14 | + | ||
15 | + private static final long serialVersionUID = 1L; | ||
16 | + | ||
17 | + /** | ||
18 | + * 响应业务状态,默认为200 | ||
19 | + */ | ||
20 | + private String code; | ||
21 | + | ||
22 | + /** | ||
23 | + * 响应消息 | ||
24 | + */ | ||
25 | + private String msg; | ||
26 | + | ||
27 | + /** | ||
28 | + * 错误消息内容 | ||
29 | + */ | ||
30 | + private String error; | ||
31 | + | ||
32 | + /** | ||
33 | + * 数据总条数 | ||
34 | + */ | ||
35 | + private Integer total; | ||
36 | + | ||
37 | + /** | ||
38 | + * 响应数据 | ||
39 | + */ | ||
40 | + private T data; | ||
41 | + | ||
42 | + /** | ||
43 | + * JWT令牌 | ||
44 | + */ | ||
45 | + private String jwtToken; | ||
46 | + | ||
47 | + | ||
48 | + /** | ||
49 | + * 无参,构造方法 | ||
50 | + */ | ||
51 | + public ResultJson() | ||
52 | + { | ||
53 | + } | ||
54 | + | ||
55 | + | ||
56 | + /** | ||
57 | + * 定义有参构造器 | ||
58 | + * | ||
59 | + * @param code 响应状态 | ||
60 | + * @param msg 响应消息 | ||
61 | + */ | ||
62 | + public ResultJson(String code, String msg) | ||
63 | + { | ||
64 | + this.code = code; | ||
65 | + this.msg = msg; | ||
66 | + } | ||
67 | + | ||
68 | + /** | ||
69 | + * 定义有参构造器 | ||
70 | + * | ||
71 | + * @param code 响应状态 | ||
72 | + * @param msg 响应消息 | ||
73 | + * @param data 响应数据 | ||
74 | + */ | ||
75 | + public ResultJson(String code, String msg, T data) | ||
76 | + { | ||
77 | + this.code = code; | ||
78 | + this.msg = msg; | ||
79 | + this.data = data; | ||
80 | + } | ||
81 | + | ||
82 | + public ResultJson(String code, String msg, T data, Integer total) | ||
83 | + { | ||
84 | + this.code = code; | ||
85 | + this.msg = msg; | ||
86 | + this.data = data; | ||
87 | + this.total = total; | ||
88 | + } | ||
89 | + | ||
90 | + public String getCode() | ||
91 | + { | ||
92 | + return code; | ||
93 | + } | ||
94 | + | ||
95 | + public void setCode(String code) | ||
96 | + { | ||
97 | + this.code = code; | ||
98 | + } | ||
99 | + | ||
100 | + public String getMsg() | ||
101 | + { | ||
102 | + return msg; | ||
103 | + } | ||
104 | + | ||
105 | + public void setMsg(String msg) | ||
106 | + { | ||
107 | + this.msg = msg; | ||
108 | + } | ||
109 | + | ||
110 | + public String getError() | ||
111 | + { | ||
112 | + return error; | ||
113 | + } | ||
114 | + | ||
115 | + public void setError(String error) | ||
116 | + { | ||
117 | + this.error = error; | ||
118 | + } | ||
119 | + | ||
120 | + public Integer getTotal() | ||
121 | + { | ||
122 | + return total; | ||
123 | + } | ||
124 | + | ||
125 | + public void setTotal(Integer total) | ||
126 | + { | ||
127 | + this.total = total; | ||
128 | + } | ||
129 | + | ||
130 | + public T getData() | ||
131 | + { | ||
132 | + return data; | ||
133 | + } | ||
134 | + | ||
135 | + public void setData(T data) | ||
136 | + { | ||
137 | + this.data = data; | ||
138 | + } | ||
139 | + | ||
140 | + public String getJwtToken() | ||
141 | + { | ||
142 | + return jwtToken; | ||
143 | + } | ||
144 | + | ||
145 | + public void setJwtToken(String jwtToken) | ||
146 | + { | ||
147 | + this.jwtToken = jwtToken; | ||
148 | + } | ||
149 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.utils; | ||
2 | + | ||
3 | +import java.util.ArrayList; | ||
4 | +import java.util.List; | ||
5 | +import java.util.Map; | ||
6 | + | ||
7 | +import com.google.gson.Gson; | ||
8 | +import com.google.gson.GsonBuilder; | ||
9 | +import com.google.gson.JsonArray; | ||
10 | +import com.google.gson.JsonElement; | ||
11 | +import com.google.gson.JsonParser; | ||
12 | +import com.google.gson.reflect.TypeToken; | ||
13 | + | ||
14 | +/** | ||
15 | + * @author 子诚 | ||
16 | + * Description: | ||
17 | + * 时间:2020/6/5 10:49 | ||
18 | + */ | ||
19 | +public class GsonUtil { | ||
20 | + | ||
21 | + private static Gson gson = null; | ||
22 | + | ||
23 | + static { | ||
24 | + if (gson == null) { | ||
25 | + gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); | ||
26 | + } | ||
27 | + } | ||
28 | + | ||
29 | + | ||
30 | + private GsonUtil() | ||
31 | + { | ||
32 | + } | ||
33 | + | ||
34 | + /** | ||
35 | + * 将对象转成json格式 | ||
36 | + * | ||
37 | + * @param object | ||
38 | + * @return String | ||
39 | + */ | ||
40 | + public static String GsonString(Object object) | ||
41 | + { | ||
42 | + String gsonString = null; | ||
43 | + if (gson != null) { | ||
44 | + gsonString = gson.toJson(object); | ||
45 | + } | ||
46 | + return gsonString; | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * 将json转成特定的cls的对象 | ||
51 | + * | ||
52 | + * @param gsonString | ||
53 | + * @param cls | ||
54 | + * @return | ||
55 | + */ | ||
56 | + public static <T> T GsonToBean(String gsonString, Class<T> cls) | ||
57 | + { | ||
58 | + T t = null; | ||
59 | + if (gson != null) { | ||
60 | + t = gson.fromJson(gsonString, cls); | ||
61 | + } | ||
62 | + return t; | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * json字符串转成list | ||
67 | + * | ||
68 | + * @param gsonString | ||
69 | + * @param cls | ||
70 | + * @return | ||
71 | + */ | ||
72 | + public static <T> List<T> GsonToList(String gsonString, Class<T> cls) | ||
73 | + { | ||
74 | + List<T> list = null; | ||
75 | + if (gson != null) { | ||
76 | + list = gson.fromJson(gsonString, new TypeToken<List<T>>() { | ||
77 | + }.getType()); | ||
78 | + } | ||
79 | + return list; | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * json字符串转成list | ||
84 | + * | ||
85 | + * @param json | ||
86 | + * @param cls | ||
87 | + * @return | ||
88 | + */ | ||
89 | + public static <T> List<T> jsonToList(String json, Class<T> cls) | ||
90 | + { | ||
91 | + ArrayList<T> mList = new ArrayList<T>(); | ||
92 | + JsonArray array = new JsonParser().parse(json).getAsJsonArray(); | ||
93 | + for (final JsonElement elem : array) { | ||
94 | + mList.add(gson.fromJson(elem, cls)); | ||
95 | + } | ||
96 | + return mList; | ||
97 | + } | ||
98 | + | ||
99 | + /** | ||
100 | + * json字符串转成list中有map的 | ||
101 | + * | ||
102 | + * @param gsonString | ||
103 | + * @return | ||
104 | + */ | ||
105 | + public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) | ||
106 | + { | ||
107 | + List<Map<String, T>> list = null; | ||
108 | + if (gson != null) { | ||
109 | + list = gson.fromJson(gsonString, | ||
110 | + new TypeToken<List<Map<String, T>>>() { | ||
111 | + }.getType()); | ||
112 | + } | ||
113 | + return list; | ||
114 | + } | ||
115 | + | ||
116 | + /** | ||
117 | + * json字符串转成map的 | ||
118 | + * | ||
119 | + * @param gsonString | ||
120 | + * @return | ||
121 | + */ | ||
122 | + public static <T> Map<String, T> GsonToMaps(String gsonString) | ||
123 | + { | ||
124 | + Map<String, T> map = null; | ||
125 | + if (gson != null) { | ||
126 | + map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { | ||
127 | + }.getType()); | ||
128 | + } | ||
129 | + return map; | ||
130 | + } | ||
131 | +} |
1 | +package com.sunyo.wlpt.message.bus.sdk.utils; | ||
2 | + | ||
3 | +import lombok.extern.slf4j.Slf4j; | ||
4 | +import okhttp3.*; | ||
5 | +import org.apache.commons.lang3.StringUtils; | ||
6 | + | ||
7 | + | ||
8 | +import java.net.URLEncoder; | ||
9 | +import java.util.HashMap; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.concurrent.TimeUnit; | ||
12 | + | ||
13 | +/** | ||
14 | + * @author 子诚 | ||
15 | + * Description: | ||
16 | + * 时间:2020/9/3 15:22 | ||
17 | + */ | ||
18 | +@Slf4j | ||
19 | +public class OkHttpUtils { | ||
20 | + | ||
21 | + private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); | ||
22 | + private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); | ||
23 | + private static final byte[] LOCKER = new byte[0]; | ||
24 | + private static OkHttpUtils instance; | ||
25 | + private OkHttpClient okHttpClient; | ||
26 | + | ||
27 | + public OkHttpUtils() | ||
28 | + { | ||
29 | + okHttpClient = new OkHttpClient.Builder() | ||
30 | + //10秒连接超时 | ||
31 | + .connectTimeout(10, TimeUnit.SECONDS) | ||
32 | + //10m秒写入超时 | ||
33 | + .writeTimeout(10, TimeUnit.SECONDS) | ||
34 | + //10秒读取超时 | ||
35 | + .readTimeout(10, TimeUnit.SECONDS) | ||
36 | + .build(); | ||
37 | + } | ||
38 | + | ||
39 | + public static OkHttpUtils getInstance() | ||
40 | + { | ||
41 | + if (instance == null) { | ||
42 | + synchronized (LOCKER) { | ||
43 | + if (instance == null) { | ||
44 | + instance = new OkHttpUtils(); | ||
45 | + } | ||
46 | + } | ||
47 | + } | ||
48 | + return instance; | ||
49 | + } | ||
50 | + | ||
51 | + public String doGet(String url) | ||
52 | + { | ||
53 | + if (isBlankUrl(url)) { | ||
54 | + return null; | ||
55 | + } | ||
56 | + Request request = getRequestForGet(url); | ||
57 | + return commonRequest(request); | ||
58 | + } | ||
59 | + | ||
60 | + public String doGet(String url, HashMap<String, String> params) | ||
61 | + { | ||
62 | + if (isBlankUrl(url)) { | ||
63 | + return null; | ||
64 | + } | ||
65 | + Request request = getRequestForGet(url, params); | ||
66 | + return commonRequest(request); | ||
67 | + } | ||
68 | + | ||
69 | + public String doPostJson(String url, String json) | ||
70 | + { | ||
71 | + if (isBlankUrl(url)) { | ||
72 | + return null; | ||
73 | + } | ||
74 | + Request request = getRequestForPostJson(url, json); | ||
75 | + return commonRequest(request); | ||
76 | + } | ||
77 | + | ||
78 | + public String doPostForm(String url, Map<String, String> params) | ||
79 | + { | ||
80 | + if (isBlankUrl(url)) { | ||
81 | + return null; | ||
82 | + } | ||
83 | + Request request = getRequestForPostForm(url, params); | ||
84 | + return commonRequest(request); | ||
85 | + } | ||
86 | + | ||
87 | + private Boolean isBlankUrl(String url) | ||
88 | + { | ||
89 | + if (StringUtils.isBlank(url)) { | ||
90 | + log.info("url is not blank"); | ||
91 | + return true; | ||
92 | + } else { | ||
93 | + return false; | ||
94 | + } | ||
95 | + } | ||
96 | + | ||
97 | + private String commonRequest(Request request) | ||
98 | + { | ||
99 | + String re = ""; | ||
100 | + try { | ||
101 | + Call call = okHttpClient.newCall(request); | ||
102 | + Response response = call.execute(); | ||
103 | + if (response.isSuccessful()) { | ||
104 | + re = response.body().string(); | ||
105 | + log.info("request url:{};response:{}", request.url().toString(), re); | ||
106 | + } else { | ||
107 | + log.info("request failure url:{};message:{}", request.url().toString(), response.message()); | ||
108 | + } | ||
109 | + } catch (Exception e) { | ||
110 | + log.error("request execute failure", e); | ||
111 | + } | ||
112 | + return re; | ||
113 | + } | ||
114 | + | ||
115 | + private Request getRequestForPostJson(String url, String json) | ||
116 | + { | ||
117 | + RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); | ||
118 | + Request request = new Request.Builder() | ||
119 | + .url(url) | ||
120 | + .post(body) | ||
121 | + .build(); | ||
122 | + return request; | ||
123 | + } | ||
124 | + | ||
125 | + | ||
126 | + private Request getRequestForPostForm(String url, Map<String, String> params) | ||
127 | + { | ||
128 | + if (params == null) { | ||
129 | + params = new HashMap<>(); | ||
130 | + } | ||
131 | + FormBody.Builder builder = new FormBody.Builder(); | ||
132 | + if (params != null && params.size() > 0) { | ||
133 | + for (Map.Entry<String, String> entry : params.entrySet()) { | ||
134 | + builder.addEncoded(entry.getKey(), entry.getValue()); | ||
135 | + } | ||
136 | + } | ||
137 | + RequestBody requestBody = builder.build(); | ||
138 | + Request request = new Request.Builder() | ||
139 | + .url(url) | ||
140 | + .post(requestBody) | ||
141 | + .build(); | ||
142 | + return request; | ||
143 | + } | ||
144 | + | ||
145 | + private Request getRequestForGet(String url, HashMap<String, String> params) | ||
146 | + { | ||
147 | + Request request = new Request.Builder() | ||
148 | + .url(getUrlStringForGet(url, params)) | ||
149 | + .build(); | ||
150 | + return request; | ||
151 | + } | ||
152 | + | ||
153 | + private Request getRequestForGet(String url) | ||
154 | + { | ||
155 | + Request request = new Request.Builder() | ||
156 | + .url(url) | ||
157 | + .build(); | ||
158 | + return request; | ||
159 | + } | ||
160 | + | ||
161 | + private String getUrlStringForGet(String url, HashMap<String, String> params) | ||
162 | + { | ||
163 | + StringBuilder urlBuilder = new StringBuilder(); | ||
164 | + urlBuilder.append(url); | ||
165 | + urlBuilder.append("?"); | ||
166 | + if (params != null && params.size() > 0) { | ||
167 | + for (Map.Entry<String, String> entry : params.entrySet()) { | ||
168 | + try { | ||
169 | + urlBuilder.append("&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")); | ||
170 | + } catch (Exception e) { | ||
171 | + urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue()); | ||
172 | + } | ||
173 | + } | ||
174 | + } | ||
175 | + return urlBuilder.toString(); | ||
176 | + } | ||
177 | + | ||
178 | +} |
-
请 注册 或 登录 后发表评论