学科分类
目录
Spring Cloud

参数绑定

第一个Feign程序实现的是一个不带参数的REST服务绑定。实际业务中,我们经常需要在调用服务时传递各种参数。下面我们在上一节基础上改进,使用eureka-feign-client远程调用eureka-provider的接口,此时调用eureka-provider接口时,需要传递一个不同类型的参数,具体步骤如下:

1.改造服务提供者eureka-provider

(1)在eureka-provider中创建entity包,并在该包中创建实体类User,具体如例1所示。

例1 eureka-provider\src\main\java\com\itheima\eurekaprovider\entity\User.java

 1     public class User {
 2         private String name;
 3         private Integer age;
 4         public User() { }
 5         public User(String name, Integer age) {
 6             this.name = name;
 7             this.age = age;
 8         }
 9         public String getName() {
 10             return name;
 11     }
 12     public void setName(String name) {
 13             this.name = name;
 14     }
 15     public Integer getAge() {
 16             return age;
 17      }
 18      public void setAge(Integer age) {
 19             this.age = age;
 20        }
 21      @Override
 22      public String toString() {
 23            return "name="+name+",age="+age;
 24       } 
 25     }

(2)在HelloController中新增3个不同类型参数的方法,修改后的HelloController代码如例2所示。

例2 eureka-provider\src\main\java\com\itheima\eurekaprovider\controller\HelloController.java

 1     import com.itheima.eurekaprovider.entity.User;
 2     import org.springframework.web.bind.annotation.*;
 3     @RestController
 4     public class HelloController {
 5          @RequestMapping(value = "/hello1",method = RequestMethod.GET)
 6           public String hello(@RequestParam String name){
 7                 return "hello,"+name;
 8           }
 9          @RequestMapping(value = "/hello2",method = RequestMethod.GET)
 10       public User hello(
 11               @RequestParam String name,@RequestHeader Integer age ){
 12            return new User(name,age);
 13       }
 14     @RequestMapping(value = "/hello3",method = RequestMethod.POST)
 15      public String hello(@RequestBody User user){
 16            return "hello,"+user.getName()+","+user.getAge();
 17      }
 18     }

在例2中,第5-17行代码是新增的三个hello()方法,它们需要传入的参数类型和参数个数不同,其中第5-8行的hello()方法使用@RequestParam传入一个String类型的参数,第9-13行的hello()方法使用@RequestParam传入两个String类型的参数。第14-17行的hello()方法使用@RequestBody传入一个User对象类型的参数。后续我们将会在FeignController中远程调用HelloController中的这些方法。

2.改造含有Feign的eureka-feign-client

(1)在eureka-feign-client中创建entity包,并在该包中创建和例4-7相同的实体类User。

(2)在FeignService中增加接口的绑定声明,修改后的FeignService代码如例3所示。

例3 eureka-feign-client\src\main\java\com\itheima\eurekafeignclient\service\FeignService.java

 1     import com.itheima.eurekafeignclient.entity.User;
 2     import org.springframework.cloud.openfeign.FeignClient;
 3     import org.springframework.stereotype.Service;
 4     import org.springframework.web.bind.annotation.*;
 5     @Service
 6     @FeignClient(name = "eureka-provider")
 7     public interface FeignService {
 8              @RequestMapping(value = "/hello1",method = RequestMethod.GET)
 9              public String hello(@RequestParam"name") String name);
 10         @RequestMapping(value = "/hello2",method = RequestMethod.GET)
 11         public User hello(@RequestParam"name") String name,@RequestHeader"age") int age);
 12         @RequestMapping(value = "/hello3",method = RequestMethod.POST)
 13         public String hello(@RequestBody User user);
 14     }

(3)在FeignController中新增HelloUser()方法处理“/helloUser”请求,该方法调用了FeignService绑定的接口,修改后的FeignController代码如例4所示。

例4 eureka-feign-client\src\main\java\com\itheima\eurekafeignclient\controller\FeignController.java

 1     import com.itheima.eurekafeignclient.entity.User;
 2     import com.itheima.eurekafeignclient.service.FeignService;
 3     import org.springframework.beans.factory.annotation.Autowired;
 4     import org.springframework.web.bind.annotation.RequestMapping;
 5     import org.springframework.web.bind.annotation.RequestMethod;
 6     import org.springframework.web.bind.annotation.RestController;
 7     @RestController
 8     public class FeignController{
 9         @Autowired
 10         FeignService feignService;
 11         @RequestMapping(value = "/helloUser",method = RequestMethod.GET)
 12         public String helloUser(){
 13            StringBuilder sb=new StringBuilder();
 14            sb.append(feignService.hello("czbk")).append("<BR/>");
 15            sb.append(feignService.hello(new User("czbk ",30)))
 16                             .append("<BR/>");
 17            sb.append(feignService.hello("czbk ",30))
 18                             .append("<BR/>");
 19             return sb.toString();
 20         }
 21     }

3.启动服务并进行测试

依次启动eureka-server、eureka-provider、eureka-feign-client,启动成功后,使用浏览器访问http://localhost:8764/helloUser,效果如图1所示。

图1 浏览器效果图

点击此处
隐藏目录