场景
SpringBoot项目中通过后台Controller向某服务接口发送POST请求。
实现
在项目pom.xml中添加依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
在util包下新建HttpRequestUtil.java
public class HttpRequestUtil {
/**
* 定义全局OkHttpClient对象
*/
private static final OkHttpClient httpClient