springboot 集成okhttp3
时间: 2023-11-14 18:09:02 浏览: 231
好的,关于springboot集成okhttp3,你可以按照以下步骤进行操作:
1. 在pom.xml文件中添加okhttp3依赖:
```xml
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
```
2. 创建OkHttpClient实例:
```java
@Configuration
public class OkHttpConfig {
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient();
}
}
```
3. 在需要使用OkHttpClient的地方注入该实例,并使用它发送请求:
```java
@RestController
public class MyController {
@Autowired
private OkHttpClient okHttpClient;
@GetMapping("/test")
public String test() throws IOException {
Request request = new Request.Builder()
.url("https://www.example.com")
.build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
}
```
阅读全文