Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of passing parameters by SpringCloud feign object and ordinary parameters and the pits encountered

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the "SpringCloud feign object parameters and ordinary parameters and how to solve the pit encountered", in the daily operation, I believe that many people in the SpringCloud feign object parameters and ordinary parameters and the pit encountered on how to solve the problem, the editor consulted all kinds of data, sorted out a simple and useful method of operation, hope to answer the "SpringCloud feign object parameters and ordinary parameters and how to solve the pit encountered" doubt! Next, please follow the editor to study!

Parameters passed by feign objects, ordinary parameters and pits encountered

Use @ RequestBody to specify the parameter passing object

@ RequestMapping (value = "/ v2/matterCode/genCode", method = RequestMethod.POST) ResultResponse getCode (@ RequestBody MatterCodeBO matterCodeBO)

Note: @ RequestBody has one and only one in a method, not both!

Ordinary transmission of parameters

Ordinary parameters are passed using @ RequestParam to specify parameters

@ RequestMapping (value = "taskApiController/getAll", method = RequestMethod.POST) List getAll (@ RequestParam ("name") String name)

Note: @ RequestParam must specify a parameter name ("name"), otherwise an error will be reported: RequestParam.value () was empty on parameter 0

Pit encountered-1

First of all, it is emphasized again that Feign invokes the service through the http protocol. The key point is to understand this sentence.

If methods in FeignClient have @ PostMapping annotations, the annotations of corresponding methods in microservice TaskApiController should also be consistent with @ PostMapping. If inconsistent, an error of 404 will be reported.

If the call fails, its circuit breaker mechanism will be triggered. If @ FeignClient (fallback = TaskFeignClientDegraded.class) is not written in @ FeignClient, an error will be reported directly.

11VOG 35.686 [http-apr-8086-exec-8] DEBUG c.b.p.m.b.c.AbstractBaseController-Got an exception

Com.netflix.hystrix.exception.HystrixRuntimeException: TaskFeignClient#getAll () failed and no fallback available.

At com.netflix.hystrix.AbstractCommand$22.call (AbstractCommand.java:819)

At com.netflix.hystrix.AbstractCommand$22.call (AbstractCommand.java:804)

Pit encountered-2

Error message:

Feign.FeignException:status 400 reading xxx#xxxx (String); content:

Reason: because of the @ ReqestParam annotation used by the feign method, the length of the received parameter is too long, and even if the feign uses the post method, the parameter will be placed after the request address, not in the request body.

Solution: encapsulate the parameters into objects and use @ RequestBody to indicate the parameters

Feign pass complex parameter objects need to pay attention to the need to pass complex parameter objects need to use Post

Also note that Feign does not support the use of GetMapping and PostMapping

@ RequestMapping (value= "user/save", method=RequestMethod.POST) in the process of transmission

Complex objects are annotated with @ RequestBody, and the receiver also needs to use the annotation @ RequestBody.

I ran into a pit:

If the consumer uses @ RequestBody and the server does not receive @ RequestBody, the parameter reception will be incomplete.

/ / consumer @ RequestMapping (value= "user/save", method=RequestMethod.POST) public User save (@ RequestBody User user); / / server @ PostMapping ("save") public User save (@ RequestBody User user) {System.out.println (user); return UserService.save (user) } at this point, the study on "SpringCloud feign object parameters and general parameters and how to solve the holes encountered" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report