网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
<artifactId>httpmime</artifactId>
<version>${apache-httpclient.version}</version>
</dependency>
<!-- Apache Http End -->
<!-- Common Begin -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!-- Common End -->
<!--mybatis-plus代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-generator.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- Compiler 插件, 设定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<!-- Add directory entries -->
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- resource -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- install -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<!-- clean -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- ant -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
<!-- dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- Java Document Generate -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- YUI Compressor (CSS/JS压缩) -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
<linebreakpos>30000</linebreakpos>
<force>true</force>
<includes>
<include>**/*.js</include>
<include>**/*.css</include>
</includes>
<excludes>
<exclude>**/*.min.js</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
#### 2.2、Maven托管
将其交给`Maven`托管
![在这里插入图片描述](https://img-blog.csdnimg.cn/36ec649be3fd4c0e980f8215b61a3d4a.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/194d32af5a8240b485f5f13d333e6283.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/70a4e29b4d7d40aa8b08e14c87ce5c34.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/51204fe9f7f44135a56aca8ab6c3365c.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c72339cd0af448381e18120f4db20ff.jpeg#pic_center)
### 三、创建通用模块
#### 3.1、创建通用模块工程
创建一个名称为 `my-project-common`的项目,该项目负责通用的类库以及工具类
`pom.xml`内容如下:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<parent>
<groupId>com.micromaple</groupId>
<artifactId>my-project-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-project-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-project-common</artifactId>
<packaging>jar</packaging>
<name>my-project-common</name>
<dependencies>
<!-- Commons Begin-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- Commons End-->
<!-- Environment Begin -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<!-- Environment End -->
<!-- Apache Http Begin -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<!-- Apache Http End -->
</dependencies>
#### 3.2、Maven托管
可以使用右侧的`Maven` 的 `+` 号来进行项目的托管。
![在这里插入图片描述](https://img-blog.csdnimg.cn/e6d964c9663c4e17aecf822b0df36b88.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/48820b316d13497db8a59176725852fd.jpeg#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/53fc71f01ed14ddebadaa061c059949b.jpeg#pic_center)
#### 3.3、完善目录结构
创建 `src/main/java` 目录后,创建包名,包名为:`com.micromaple.my.project.common`
此工程是放一些通用的类库或者是工具类之类的。
#### 3.4、定义通用类库
##### 3.4.1、返回状态码
创建常量包`constant`。
通用常量返回状态码`HttpStatus`
package com.micromaple.my.project.common.constant;
/**
* 返回状态码
* Title: HttpStatus
* Description:
*
* @author Micromaple
*/
public class HttpStatus {
/**
* 操作成功
*/
public static final int SUCCESS = 200;
/\*\*
* 对象创建成功
*/
public static final int CREATED = 201;
/\*\*
* 请求已经被接受
*/
public static final int ACCEPTED = 202;
/\*\*
* 操作已经执行成功,但是没有返回数据
*/
public static final int NO_CONTENT = 204;
/\*\*
* 资源已被移除
*/
public static final int MOVED_PERM = 301;
/\*\*
* 重定向
*/
public static final int SEE_OTHER = 303;
/\*\*
* 资源没有被修改
*/
public static final int NOT_MODIFIED = 304;
/\*\*
* 参数列表错误(缺少,格式不匹配)
*/
public static final int BAD_REQUEST = 400;
/\*\*
* 未授权
*/
public static final int UNAUTHORIZED = 401;
/\*\*
* 访问受限,授权过期
*/
public static final int FORBIDDEN = 403;
/\*\*
* 资源,服务未找到
*/
public static final int NOT_FOUND = 404;
/\*\*
* 不允许的http方法
*/
public static final int BAD_METHOD = 405;
/\*\*
* 资源冲突,或者资源被锁
*/
public static final int CONFLICT = 409;
/\*\*
* 不支持的数据,媒体类型
*/
public static final int UNSUPPORTED_TYPE = 415;
/\*\*
* 系统内部错误
*/
public static final int ERROR = 500;
/\*\*
* 接口未实现
*/
public static final int NOT_IMPLEMENTED = 501;
}
##### 3.4.2、通用返回结果
创建数据传输包`dto`。
通用数据传输返回结果`BaseResult`
package com.micromaple.my.project.common.dto;
import com.micromaple.my.project.common.constant.HttpStatus;
import java.util.HashMap;
/**
* 返回结果
* Title: BaseResult
* Description:
*
* @author Micromaple
*/
public class BaseResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
/\*\*
* 状态码
*/
public static final String CODE_TAG = “code”;
/\*\*
* 返回内容
*/
public static final String MSG_TAG = “msg”;
/\*\*
* 数据对象
*/
public static final String DATA_TAG = “data”;
/\*\*
* 初始化一个新创建的 BaseResult 对象,使其表示一个空消息。
*/
public BaseResult() {
}
/\*\*
* 初始化一个新创建的 BaseResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public BaseResult(int code, String msg) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/\*\*
* 初始化一个新创建的 BaseResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public BaseResult(int code, String msg, Object data) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
super.put(DATA_TAG, data);
}
/\*\*
* 返回成功消息
*
* @return 成功消息
*/
public static BaseResult success() {
return BaseResult.success(“操作成功”);
}
/\*\*
* 返回成功数据
*
* @return 成功消息
*/
public static BaseResult success(Object data) {
return BaseResult.success(“操作成功”, data);
}
/\*\*
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static BaseResult success(String msg) {
return BaseResult.success(msg, null);
}
/\*\*
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static BaseResult success(String msg, Object data) {
return new BaseResult(HttpStatus.SUCCESS, msg, data);
}
/\*\*
* 返回错误消息
*
* @return
*/
public static BaseResult error() {
return BaseResult.error(“操作失败”);
}
/\*\*
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static BaseResult error(String msg) {
return BaseResult.error(msg, null);
}
/\*\*
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static BaseResult error(String msg, Object data) {
return new BaseResult(HttpStatus.ERROR, msg, data);
}
/\*\*
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static BaseResult error(int code, String msg) {
return new BaseResult(code, msg, null);
}
}
##### 3.4.3、通用工具类
创建工具类包`utils`。
通用`Jackson`工具类`MapperUtils`
package com.micromaple.my.project.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Jackson 工具类
* Title: MapperUtils
* Description:
*
* @author Micromaple
*/
public class MapperUtils {
private final static ObjectMapper objectMapper = new ObjectMapper();
public static ObjectMapper getInstance() {
return objectMapper;
}
/\*\*
* 转换为 JSON 字符串
*
* @param obj
* @return
* @throws Exception
*/
public static String obj2json(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return “”;
}
/\*\*
* 转换为 JSON 字符串,忽略空值
*
* @param obj
* @return
* @throws Exception
*/
public static String obj2jsonIgnoreNull(Object obj) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(obj);
}
/\*\*
* 转换为 JavaBean
*
* @param jsonString
* @param clazz
* @return
* @throws Exception
*/
public static T json2pojo(String jsonString, Class clazz){
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
try{
return objectMapper.readValue(jsonString, clazz);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/\*\*
* 字符串转换为 Map<String, Object>
*
* @param jsonString
* @return
* @throws Exception
*/
public static Map<String, Object> json2map(String jsonString){
if(StringUtils.isBlank(jsonString)) return null;
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
return mapper.readValue(jsonString, Map.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/\*\*
* 深度转换 JSON 成 Map
*
* @param json
* @return
*/
public static Map<String, Object> json2mapDeeply(String json) throws Exception {
return json2MapRecursion(json, objectMapper);
}
/\*\*
* 把 JSON 解析成 List,如果 List 内部的元素存在 jsonString,继续解析
*
* @param json
* @param mapper 解析工具
* @return
* @throws Exception
*/
private static List json2ListRecursion(String json, ObjectMapper mapper) throws Exception {
if (json == null) {
return null;
}
List<Object> list = mapper.readValue(json, List.class);
for (Object obj : list) {
if (obj != null && obj instanceof String) {
String str = (String) obj;
if (str.startsWith("[")) {
obj = json2ListRecursion(str, mapper);
} else if (obj.toString().startsWith("{")) {
obj = json2MapRecursion(str, mapper);
}
}
}
return list;
}
/\*\*
* 把 JSON 解析成 Map,如果 Map 内部的 Value 存在 jsonString,继续解析
*
* @param json
* @param mapper
* @return
* @throws Exception
*/
private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper) throws Exception {
if (json == null) {
return null;
}
Map<String, Object> map = mapper.readValue(json, Map.class);
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object obj = entry.getValue();
if (obj != null && obj instanceof String) {
String str = ((String) obj);
if (str.startsWith("[")) {
List<?> list = json2ListRecursion(str, mapper);
map.put(entry.getKey(), list);
} else if (str.startsWith("{")) {
Map<String, Object> mapRecursion = json2MapRecursion(str, mapper);
map.put(entry.getKey(), mapRecursion);
}
}
}
return map;
}
/\*\*
* 将 JSON 数组转换为集合
*
* @param jsonArrayStr
* @param clazz
* @return
* @throws Exception
*/
public static List json2list(String jsonArrayStr, Class clazz) throws Exception {
JavaType javaType = getCollectionType(ArrayList.class, clazz);
List list = (List) objectMapper.readValue(jsonArrayStr, javaType);
return list;
}
/\*\*
* 获取泛型的 Collection Type
*
* @param collectionClass 泛型的Collection
* @param elementClasses 元素类
* @return JavaType Java类型
* @since 1.0
*/
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>… elementClasses) {
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
/\*\*
* 将 Map 转换为 JavaBean
*
* @param map
* @param clazz
* @return
*/
public static T map2pojo(T map, Class clazz) {
return objectMapper.convertValue(map, clazz);
}
/\*\*
* 将 Map 转换为 JSON
*
* @param map
* @return
*/
public static String mapToJson(Map map) {
try {
return objectMapper.writeValueAsString(map);
} catch (Exception e) {
e.printStackTrace();
}
return “”;
}
/\*\*
* 将 JSON 对象转换为 JavaBean
*
* @param obj
* @param clazz
* @return
*/
public static T obj2pojo(Object obj, Class clazz) {
return objectMapper.convertValue(obj, clazz);
}
}
`HttpClient` 工具类
package com.micromaple.my.project.common.utils;
import com.google.common.collect.Maps;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
/**
* HttpClient 工具类
* Title: HttpClientUtils
* Description:
*
* @author Micromaple
*/
public class HttpClientUtils {
public static final String GET = "get";
public static final String POST = "post";
public static final String REQUEST_HEADER_CONNECTION = "keep-alive";
public static final String REQUEST_HEADER_CONTENTTYPE = "application/json;charset=UTF-8";
public static final String REQUEST_HEADER_CONSUMES = "application/json";
//设置连接超时时间,单位毫秒。
public static final int CONNECTTIMEOUT = 18000;
//请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
public static final int SOCKETTIMEOUT = 60000;
//设置从connect Manager获取Connection 超时时间,单位毫秒
public static final int CONNECTIONREQUESTTIMEOUT = 18000;
/\*\*
* GET 请求
*
* @param url 请求地址
* @return
*/
public static String doGet(String url) {
return createRequest(url, GET, null, false, null, 0);
}
/\*\*
* GET 请求 - 代理
*
* @param url 请求地址
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @return
*/
public static String doGet(String url, boolean useProxy, String proxyHost, int proxyPort) {
return createRequest(url, GET, null, useProxy, proxyHost, proxyPort);
}
/\*\*
* GET 请求
*
* @param url 请求地址
* @param cookie cookie
* @return
*/
public static String doGet(String url, String cookie) {
return createRequest(url, GET, cookie, false, null, 0);
}
/\*\*
* POST 请求
*
* @param url 请求地址
* @param params 请求参数(可选)
* @return
*/
public static String doPost(String url, BasicNameValuePair… params) {
return createRequest(url, POST, null, false, null, 0, params);
}
/\*\*
* POST 请求 - 代理
*
* @param url 请求地址
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @param params 请求参数(可选)
* @return
*/
public static String doPost(String url, boolean useProxy, String proxyHost, int proxyPort, BasicNameValuePair… params) {
return createRequest(url, POST, null, useProxy, proxyHost, proxyPort, params);
}
/\*\*
* POST 请求
*
* @param url 请求地址
* @param cookie cookie
* @param params 请求参数(可选)
* @return
*/
public static String doPost(String url, String cookie, BasicNameValuePair… params) {
return createRequest(url, POST, cookie, false, null, 0, params);
}
/\*\*
* POST 请求 - 文本 方式
*
* @param url 请求地址
* @param jsonBodyParams 请求参数 (JSON 格式)
* @return
*/
public static String doPostForRow(String url, String jsonBodyParams) {
return createRequestForRow(url, jsonBodyParams, false, null, 0);
}
/\*\*
* POST 请求 - 文本 方式 - 代理
*
* @param url 请求地址
* @param jsonBodyParams 请求参数
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @return
*/
public static String doPostForRow(String url, String jsonBodyParams, boolean useProxy, String proxyHost, int proxyPort) {
return createRequestForRow(url, jsonBodyParams, useProxy, proxyHost, proxyPort);
}
/\*\*
* POST 请求
*
* @param url 请求地址
* @param pMap 请求参数
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @return
*/
public static Map<String, Object> doPost(String url, Map<String, String> pMap, boolean useProxy, String proxyHost, int proxyPort) {
String str = HttpClientUtils.doPost(url, useProxy, proxyHost, proxyPort, generatNameValuePair(pMap));
Map<String, Object> rtnMap = Maps.newHashMap();
try {
rtnMap = MapperUtils.json2map(str);
} catch (Exception e) {
e.printStackTrace();
}
return rtnMap;
}
/\*\*
* 创建请求
*
* @param url 请求地址
* @param requestMethod 请求方式 GET/POST
* @param cookie cookie
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @param params 请求参数 仅限于 POST 请求用
* @return
*/
private static String createRequest(String url, String requestMethod, String cookie, boolean useProxy, String proxyHost, int proxyPort, BasicNameValuePair… params) {
//创建 HTTPClient 客户端
// CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient = getHttpClient(useProxy, proxyHost, proxyPort);
String result = null;
try {
//请求结果
result = null;
//请求方式
HttpGet httpGet = null;
HttpPost httpPost = null;
//响应
CloseableHttpResponse httpResponse = null;
// GET 请求
if (GET.equals(requestMethod)) {
httpGet = new HttpGet(url);
httpGet.setHeader("Connection", REQUEST_HEADER_CONNECTION);
httpGet.setHeader("Cookie", cookie);
httpResponse = httpClient.execute(httpGet);
}
// POST 请求
else if (POST.equals(requestMethod)) {
httpPost = new HttpPost(url);
httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
httpPost.setHeader("Cookie", cookie);
//有参数进来
if (params != null && params.length > 0) {
httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), "UTF-8"));
}
httpResponse = httpClient.execute(httpPost);
}
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/\*\*
* 创建 ROW POST 请求
*
* @param url 请求地址
* @param jsonBodyParam 请求参数 (JSON 格式)
* @param useProxy 是否需要代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @return
*/
private static String createRequestForRow(String url, String jsonBodyParam, boolean useProxy, String proxyHost, int proxyPort) {
//创建 HTTPClient 客户端
// CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient = getHttpClient(useProxy, proxyHost, proxyPort);
String result = null;
try {
//请求结果
result = null;
//请求方式
HttpPost httpPost = null;
//响应
CloseableHttpResponse httpResponse = null;
httpPost = new HttpPost(url);
httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
httpPost.setHeader("Content-Type", REQUEST_HEADER_CONTENTTYPE);
httpPost.setHeader("consumes", REQUEST_HEADER_CONSUMES);
httpPost.setEntity(new StringEntity(jsonBodyParam, "UTF-8"));
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/\*\*
* 创建 HttpClient 客户端
*
* @param useProxy 是否代理
* @param proxyHost 代理地址
* @param proxyPort 代理端口
* @return
*/
private static CloseableHttpClient getHttpClient(boolean useProxy, String proxyHost, int proxyPort) {
CloseableHttpClient httpClient = null;
if (useProxy) {
//设置代理IP、端口、协议
HttpHost proxy = new HttpHost(proxyHost, proxyPort, “http”);
//把代理设置到请求配置
//setConnectTimeout:设置连接超时时间,单位毫秒。
//setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
//setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(CONNECTTIMEOUT)
.setSocketTimeout(SOCKETTIMEOUT)
.setConnectionRequestTimeout(CONNECTIONREQUESTTIMEOUT)
.setProxy(proxy)
.build();
//创建 HTTPClient 客户端
httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
} else {
//setConnectTimeout:设置连接超时时间,单位毫秒。
//setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
//setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(CONNECTTIMEOUT)
.setSocketTimeout(SOCKETTIMEOUT)
.setConnectionRequestTimeout(CONNECTIONREQUESTTIMEOUT)
.build();
//创建 HTTPClient 客户端
httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
}
return httpClient;
}
/\*\*
* MAP类型数组转换成BasicNameValuePair类型
*
* @param properties MAP类型数组
* @return BasicNameValuePair类型数组
*/
private static BasicNameValuePair[] generatNameValuePair(Map<String, String> properties) {
BasicNameValuePair[] basicNameValuePairs = new BasicNameValuePair[properties.size()];
int i = 0;
for (Map.Entry<String, String> entry : properties.entrySet()) {
basicNameValuePairs[i++] = new BasicNameValuePair(entry.getKey(), entry.getValue());
}
return basicNameValuePairs;
}
}
创建好后,目录结构效果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/3fb4d3708568469ca43fafd133e5ccc9.jpeg#pic_center)
### 四、创建项目服务模块
#### 4.1、创建项目服务模块工程
创建一个名称为 `my-project-server`的项目,该项目负责实现具体业务,以及提供服务。
`pom.xml`内容如下:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<parent>
<groupId>com.micromaple</groupId>
<artifactId>my-project-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-project-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-project-server</artifactId>
<packaging>jar</packaging>
<name>my-project-server</name>
<dependencies>
<!-- Project Begin -->
<dependency>
<groupId>com.micromaple</groupId>
<artifactId>my-project-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- Project End -->
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Spring Boot End -->
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- Environment Begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Environment End -->
<!-- Common Begin -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- Common End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
rter-validation
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- Environment Begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Environment End -->
<!-- Common Begin -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- Common End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
[外链图片转存中…(img-iNo0EuGT-1715475403923)]
[外链图片转存中…(img-lFRAT5nV-1715475403923)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!