怎么把String和feign请求的interface在枚举里映射
时间: 2023-12-10 13:39:38 浏览: 26
可以在枚举中定义一个属性,用来存储对应的接口或字符串,例如:
```java
public enum ApiEnum {
API_ONE("/api/one", ApiOneService.class),
API_TWO("/api/two", ApiTwoService.class);
private String url;
private Class<?> clazz;
ApiEnum(String url, Class<?> clazz) {
this.url = url;
this.clazz = clazz;
}
public String getUrl() {
return url;
}
public Class<?> getClazz() {
return clazz;
}
}
```
其中,`url`表示对应的请求路径,`clazz`表示对应的接口。
然后,可以通过枚举值的名称来获取对应的接口或字符串:
```java
ApiEnum api = ApiEnum.API_ONE;
String url = api.getUrl(); // "/api/one"
Class<?> clazz = api.getClazz(); // ApiOneService.class
```
相关问题
怎么把feign请求的interface在枚举里映射
可以在枚举中定义一个 `url` 字段,用于存储接口的 URL 地址。然后在定义每个枚举值时,将对应的 URL 地址赋值给该字段。例如:
```java
public enum MyApi {
GET_USER("/user/{id}", HttpMethod.GET),
ADD_USER("/user", HttpMethod.POST),
UPDATE_USER("/user/{id}", HttpMethod.PUT),
DELETE_USER("/user/{id}", HttpMethod.DELETE);
private String url;
private HttpMethod method;
MyApi(String url, HttpMethod method) {
this.url = url;
this.method = method;
}
public String getUrl() {
return url;
}
public HttpMethod getMethod() {
return method;
}
}
```
然后在使用 Feign 进行接口调用时,可以通过枚举值获取对应的 URL 地址和请求方法,然后使用 Feign 进行调用。例如:
```java
@FeignClient(name = "my-service")
public interface MyServiceClient {
@RequestMapping(method = RequestMethod.GET, value = MyApi.GET_USER.getUrl())
User getUser(@PathVariable("id") String id);
@RequestMapping(method = RequestMethod.POST, value = MyApi.ADD_USER.getUrl())
User addUser(@RequestBody User user);
@RequestMapping(method = RequestMethod.PUT, value = MyApi.UPDATE_USER.getUrl())
User updateUser(@PathVariable("id") String id, @RequestBody User user);
@RequestMapping(method = RequestMethod.DELETE, value = MyApi.DELETE_USER.getUrl())
void deleteUser(@PathVariable("id") String id);
}
```
这样就可以通过枚举来管理接口的 URL 地址,避免在代码中硬编码 URL,提高代码的可读性和可维护性。
feign远程调用如何在请求头上加上token
在Feign远程调用中,可以通过自定义请求拦截器来实现在请求头上加入Token。
首先,需要实现一个Feign的请求拦截器,代码如下:
```java
public class TokenInterceptor implements RequestInterceptor {
private final String token;
public TokenInterceptor(String token) {
this.token = token;
}
@Override
public void apply(RequestTemplate template) {
template.header("Authorization", "Bearer " + token);
}
}
```
在这个拦截器中,我们通过`RequestTemplate`对象来设置请求头,将Token添加到`Authorization`字段中。
接着,在使用Feign进行远程调用时,需要将自定义请求拦截器加入到Feign的请求拦截器列表中,代码如下:
```java
@FeignClient(name = "service-name", configuration = FeignConfig.class)
public interface RemoteService {
@RequestMapping("/test")
String test();
}
@Configuration
public class FeignConfig {
private final String token = "xxx";
@Bean
public RequestInterceptor requestInterceptor() {
return new TokenInterceptor(token);
}
}
```
在这个例子中,我们在`FeignConfig`中创建了一个`TokenInterceptor`实例,并将其加入到Feign的请求拦截器列表中。这样,在使用Feign进行远程调用时,就会自动加入Token到请求头中了。
需要注意的是,如果你的Token是动态生成的,那么就需要在每次请求前重新生成并设置到拦截器中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)