前言
@FeignClient 用于定义接口,通过该接口调用远程服务的方法
目录标题
一、使用
- 开启Feign
@EnableFeignClients
public class 启动类{略}
- 配置调用远程服务的接口
@FeignClient(
url = "https://www.test.com",
name = "test-url",
configuration = TestFeignConfiguration.class
)
public interface ITestClient {
String API_PREFIX = "/test-api";
@PostMapping(value = API_PREFIX + "/forward/{id}")
Resp<String> forward(@RequestParam String name,
@RequestParam List<String> hobbies,
@PathVariable String id);
- Feign接口的配置类 (这里在用feign调用其他服务时,在header中加上了token等)
@Configuration
public class TestFeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.header("Content-Type", "application/json; charset=UTF-8");
template.header("X-Auth-Token", "我的token啦啦啦");
}
}
- 表现层调用
@Resource
private ITestClient testClient;
@RequestMapping(value = "/forward/{id}",method = RequestMethod.POST)
public Resp<String> forward(@RequestParam String name,
@RequestParam List<String> hobbies,
@PathVariable String id){
return testClient.forward(name,hobbies,id);
}
二、@RequestParam、@RequestBody区别![在这里插入图片描述](https://img-blog.csdnimg.cn/f702ef69cf59482598de1b192cd7b5b2.png)
参数中有个content-type,指定了传参类型,默认为application/x-www-form-urlencoded
① 请求url中的参数、form-data(表单)、x-www-form-urlencoded:只可以用@RequestParam
② application/json:json字符串部分可以用@RequestBody
③GET请求不能用@RequestBody
④
RequestBody 只能有一个!!
RequestBody 只能有一个!!
RequestBody 只能有一个!!