Parameter binding
- stay The fifth chapter Feign is used to implement a REST service binding without parameters. However, in the actual business, it is much more complicated than this. Different types of parameters are passed in at various locations of HTTP requests, and the returned parameters may also be a complex structure. Here are some complicated parameter binding methods.
Specific steps
- You need to use entity classes to import lombok dependencies.
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
- Add the interface definition in service hello-client1, including Request request, Request with Header information, Request with RequestBody, and Request that returns an object.
import com.dome.entity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { private final Logger logger = LoggerFactory.getLogger(HelloController.class); /** * request request * @param name * @return */ @RequestMapping(value = "/getname",method = RequestMethod.GET) String hello(@RequestParam("name") String name){ return "hello:"+name; } /** * Information with header request * @param name * @return */ @RequestMapping(value = "/getuser",method = RequestMethod.GET) User getUser(@RequestHeader("name") String name, @RequestHeader("age") Integer age){ logger.info("getuser: name===>"+name); return new User(name,age); } /** * Request information with requestbody. * @param user * @return */ @RequestMapping(value = "/touser",method = RequestMethod.POST) public String toUser(@RequestBody User user){ return user.getName()+" "+user.getAge(); } }
- Create the entity User of the subpackage data.
import lombok.Data; @Data public class User { private String name; private Integer age; public User(){ } public User(String name, Integer age){ this.name = name; this.age = age; } }
Note: the User must have a constructor, or Feign will throw an exception when converting the User object based on the JSON string.
- First, create the same User class as above.
- Add HelloService to feign consumer service, and add the same interface as hello client1, binding declaration.
import com.dome.entity.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*; @FeignClient(value = "hello-client1") public interface HelloService { /** * request request * @param name * @return */ @RequestMapping(value = "/getname",method = RequestMethod.GET) String hello(@RequestParam("name") String name); /** * Information with header request * @param name * @return */ @RequestMapping(value = "/getuser",method = RequestMethod.GET) User getUser(@RequestHeader("name") String name, @RequestHeader("age") Integer age); /** * Request information with requestbody. * @param user * @return */ @RequestMapping(value = "/touser",method = RequestMethod.POST) String toUser(@RequestBody User user); }
Note: when defining parameter binding, @ RequestParam, @ RequestHeader, etc., you can specify the annotation of parameter name. Their value value value must not be less. In springmvc, these annotations will be the default value according to the parameter name, but in feign, the binding parameter must indicate the specific parameter name through the value attribute, otherwise an IllegalStateException exception exception will be thrown. The value attribute does not Can be empty.
- Finally, add the interface / getalluser in the controller of feign consumer to call the declared interface.
import com.dome.entity.User; import com.dome.service.HelloService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { private Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private HelloService helloService; @RequestMapping(value = "/getalluser",method = RequestMethod.GET) public String helloConsumer1(){ StringBuffer str = new StringBuffer(); str.append(helloService.hello("Li Ming")).append("\n"); str.append(helloService.getUser("mike",19)).append("\n"); str.append(helloService.toUser(new User("Li Lei",19))); logger.info("The return information is:"+str); return str.toString(); } }
Test verification
- Start the service, call the interface http://localhost:9001/getalluser, and type the following result in the console, which means the interface call is successful.