Preface
Recently, the project of microservice architecture is using feign to call between services. In the process of intermodulation, it is inevitable to have problems. According to the error summary, it is mainly caused by the error of request mode and the error of connection parameters. Here is a summary record. The following is explained by three cases: no parameter, single parameter and multi parameter. Each case is further described in two request modes: get and post. In this way, six cases cover all cases of feign calls.
One suggestion is to ensure unnecessary trouble. When writing feign interface, it is absolutely consistent with our mapping method. At the same time, neither the request method nor the request Parameter annotation is written lazily. If you follow this specification, you can avoid 90% of the call errors.
No reference case
The nonparametric case means that our method does not accept parameters.
Get request
When we only write RequestMapping and do not specify RequestMethod. The default method is a get request.
@RequestMapping("/noArgs/getDemo") public void noArgsGetDemo();
Post request
@RequestMapping(value = "/noArgs/postDemo",method = RequestMethod.POST) public void noArgsPostDemo();
You can also use PostMapping directly
@PostMapping(value = "/noArgs/postDemo") public void noArgsPostDemo();
Case of single parameter
Method has only one parameter
Get request
get request method can only use RequestParam annotation
@RequestMapping(value = "/singleArg/getDemo") public void singleArgGetDemo(@RequestParam String name);
Do not write the RequestMethod annotation. The default is get request.
Post request
There are three ways to receive parameters in post request: one is not writing, the other is RequestParam and the other is RequestBody.
RequestParam
Let's talk about RequestParam first. method needs to be specified, if not, it is the same as above. The default is get.
@RequestMapping(value = "/singleArg/PostDemo",method = RequestMethod.POST) public void singleArgPostDemo(@RequestParam String name);
RequestBody
Once the RequestBody is used, it is a post request, and there is no need to write a method.
@RequestMapping(value = "/singleArg/PostDemo") public void singleArgPostDemo(@RequestBody String name);
This annotation is very powerful. It's useless if you write post or not or get. It won't work. As long as it's in this way, it's a post request.
Nothing is written.
@RequestMapping(value = "/singleArg/PostDemo") public void singleArgPostDemo(String name);
At this time, the RequestBody annotation will be added before the parameter by default. Then it becomes a Post request.
Multi parameter
get request
Multiple parameters are also annotated with @ RequestParam.
@RequestMapping(value = "/moreArgs/getDemo") public void moreArgGetDemo(@RequestParam String name,@RequestParam String sex);
The RequestParam annotation is used, and the default method is get.
post request
Only one of multiple parameters can be in the requestBody mode, and other parameters should be in the requestParam mode.
@RequestMapping(value = "/moreArgs/postDemo") public void moreArgPostDemo(@RequestBody String name,@RequestParam String sex);
You can also use RequestParam mode for all, but you need to specify post.
@RequestMapping(value = "/moreArgs/postDemo",method = RequestMethod.POST) public void moreArgPostDemo(@RequestParam String name,@RequestParam String sex);
If no annotation is written before the parameter, an error will be reported, because two requestbodies will be added by default.
summary
So far, feign's request mode is clear.