springboot封装http请求
时间: 2023-09-19 18:06:57 浏览: 53
在Spring Boot中封装HTTP请求可以使用RestTemplate或者Feign来实现。下面分别介绍这两种方式的使用:
1. 使用RestTemplate:
RestTemplate是Spring提供的一个HTTP客户端工具,可以发送HTTP请求并处理响应。以下是一个简单的封装示例:
```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class HttpUtils {
private static final RestTemplate restTemplate = new RestTemplate();
public static <T> T sendGetRequest(String url, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
return responseEntity.getBody();
}
public static <T> T sendPostRequest(String url, Object request, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
HttpEntity<Object> entity = new HttpEntity<>(request, headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
return responseEntity.getBody();
}
}
```
上述代码中,我们定义了一个HttpUtils类,提供了sendGetRequest和sendPostRequest两个方法,分别用于发送GET和POST请求,并且可以指定返回结果的类型。
2. 使用Feign:
Feign是一个声明式的Web服务客户端,可以通过注解方式定义和实现HTTP请求。以下是一个简单的封装示例:
1)首先,在Spring Boot的启动类上添加@EnableFeignClients注解:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
2)然后,创建一个Feign客户端接口:
```java
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = "example", url = "http://example.com")
public interface HttpClient {
@GetMapping("/api/get")
String get();
@PostMapping("/api/post")
String post(@RequestBody Object request);
}
```
在上述代码中,我们使用@FeignClient注解指定了服务名和请求URL。
3)最后,在需要使用HTTP请求的地方注入HttpClient接口并调用相应的方法:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final HttpClient httpClient;
@Autowired
public MyService(HttpClient httpClient) {
this.httpClient = httpClient;
}
public String doHttpRequest() {
String response = httpClient.get();
// 处理响应
return response;
}
}
```
在上述代码中,我们通过@Autowired注解将HttpClient接口注入到MyService中,并调用需要的方法来发送HTTP请求。
以上是在Spring Boot中封装HTTP请求的两种常用方式,你可以根据具体需求选择使用。