在Spring Cloud (十)、声明式服务调用Feign(入门)的基础上,我们进行Feign的参数绑定的操作。
一、在服务提供方(hello-service)和服务消费方(fegin-consume)的工程上分别添加User实体:
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
需要注意的是,这里必须要提供有User的默认构造函数。不然,Spring Cloud Feign根据JSON字符串转换User对象时会拍哦出异常。
二、扩展服务提供方hello-service的HelloController内容:
/*带有Request参数的请求*/
@RequestMapping("/hello1")
public String hello(@RequestParam String name){
return "hello "+name;
}
/*带有Header信息的请求*/
@RequestMapping("/hello2")
public User hello(@RequestHeader String name,@RequestHeader Integer age){
return new User(name,age);
}
/*带有RequestBody的请求以及请求响应体是一个对象的请求*/
@RequestMapping("/hello3")
public String hello(@RequestBody User user){
return "hello "+user.getName()+", "+user.getAge();
}
三、在服务消费方的HelloService接口中增加的对服务提供方三个接口绑定声明,修改后,HelloService接口的内容如下:
@FeignClient("hello-service")
@Service
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping("/hello1")
String hello(@RequestParam("name") String name);
@RequestMapping("/hello2")
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping("/hello3")
String hello(@RequestBody User user);
}
这里一定要注意,在定义各参数绑定的时候@RequestParam、@RequestHeader等可以指定参数名称的注解,他们的value值千万不能少。在Spring MVC程序中,这些注解会根据参数名称来作为默认值,但是在Feign中绑定参数必须通过value属性来指明具体的参数名,不然会抛出IllegalStateException异常,value属性不能为空。
四、在ConsumerController中新增一个/feign-consumer2接口,来对HelloService接口中新增的声明接口进行调用,代码内容如下:
@RestController
public class ConsumerController {
private Logger logger = Logger.getLogger(String.valueOf(ConsumerController.class));
@Autowired
HelloService helloService;
@RequestMapping("/feign-consumer")
public String helloConsumer(){
logger.info("=========helloConsumer=========");
return helloService.hello();
}
@RequestMapping("/feign-consumer2")
public String helloConsumer2(){
StringBuilder sb = new StringBuilder();
sb.append(helloService.hello()).append("\n");
sb.append(helloService.hello("HDN")).append("\n");
sb.append(helloService.hello("HDN",20)).append("\n");
sb.append(helloService.hello(new User("HDN",20))).append("\n");
return sb.toString();
}
}
五、测试验证
分别启动服务注册中心、两个hello-service(8081和8082)和fegn-consume,在postman中发送请求http: localhost:9001/feign-consumer2,可以看到:
-----------------------------------以下方法将分别进行测试-----------------------------
在上面的基础上,在ConsumerController中实现三个接口,分别进行访问:
@RequestMapping("/feign-requestParam")
public String getRequestParam(@RequestParam String name){
return helloService.hello(name);
}
@RequestMapping("/feign-requestHeader")
public User getRequestHeader(@RequestHeader String name,@RequestHeader Integer age){
return helloService.hello(name,age);
}
@RequestMapping("/feign-requestBody")
public String getRequestBody(@RequestBody User user){
return helloService.hello(user);
}
1、访问http://localhost:9001/feign-requestParam?name=hdn
2、访问http://localhost:9001/feign-requestHeader
3、访问http://localhost:9001/feign-requestBody