web123456

Complex parameter passing when SpringCloud uses feign

Feign transmission attention


 Recently, I have tried to refactor the previous project with SpringCloud and used Feign client components to call microservices. There are often problems that cannot pass parameters and become null. I checked online and found that there are still certain restrictions on the use of feign in parameters. The main thing is to pay attention to:


 1. When the parameters are complex, feign will force the post request even if it is declared as a get request.

 2. @GetMapping similar annotation declaration request is not supported. @RequestMapping(value = "url",method = ) is required.

 3. When using @RequestParam annotation, you must add parameter names afterwards


 I wrote a simple case, and simultaneously transmit an object and a string as request parameters, mainly focusing on the declaration of parameters. Please refer to the relevant knowledge about the dependencies and configurations of eureka and feign, such as the following information. For reference only:


Server side (producer)


Prepare:
 1. It has been configured to register the service ineureka, service name is "item-service"
2. Service layer and dao layer have been implemented

Logic: ItemController receives parameters, calls the service layer, adds details desc to the product Item object in the service, and then calls dao to save the Item object, return the Result result encapsulation object

@RestController
 public class ItemController {

 @Autowired
 private ItemService itemService;

 /**
      * Add to
 * @param item
 * @param desc
 * @return
 */
 @RequestMapping("/item/save")
 public Result addItem(@RequestBody Item item, @RequestParam("desc") String desc){
 return (item, desc);
     }
 }


Client (consumer)


Prepare:Registered to eureka

logic:
 Declares ItemFeignClient to call a service named "item-service" on eureka, and returns a Result object
 Using Post request, pass two parameters:
  1. TbItem object, declared using @RequestBody
  2. String string, declared using @RequestParam("xxx")
@FeignClient("item-service")
 public interface ItemFeignClient {

     /**
      * Add to
      * @param item
      * @param desc
      * @return
      */
     @RequestMapping(value = "/item/save",method = )
     Result addItem(@RequestBody TbItem item, @RequestParam("desc") String desc);