In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the contextId attribute in the @ FeignClient annotation in Spring Cloud, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
The contextId attribute in the @ FeignClient annotation
Before using the @ FeignClient annotation, we need to introduce its related dependencies, version 3.0.1
Org.springframework.cloud spring-cloud-starter-openfeign 3.0.1
For example, we have a user service, and there are many interfaces in the user service. We call the interface through @ FeignClient. We don't want to define all the calling interfaces in one interface class, so we build the following two Feign interface classes:
FeignClient (name = "user-server") public interface UserServerClient1 {@ GetMapping ("/ user/get") public User getUser (@ RequestParam ("id") int id);} @ FeignClient (name = "user-server") public interface UserServerClient2 {@ GetMapping ("/ user/getAll") public List getAllUser ();}
When you start the project in this case, the project will report an error because the name of the Bean conflicts, as shown below:
Description:
The bean 'user-server.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Solution one
Add the following configuration to the yml configuration file to allow BeanDefinition like beanName to appear
Spring: main: allow-bean-definition-overriding: true solution II
The purpose of manually specifying a different contextId,contextId for each FeignClient is to distinguish between FeignClient instances
@ FeignClient (contextId = "userService1", name = "user-server") public interface UserServerClient1 {@ GetMapping ("/ user/get") public User getUser (@ RequestParam ("id") int id);} @ FeignClient (contextId = "userService1", name = "user-server") public interface UserServerClient2 {@ GetMapping ("/ user/getAll") public List getAllUser ();} FeignClient Notes and Parameter issues
@ FeignClient is a common annotation and an important feature in the development process using the SpringCloud technology of the distributed architecture SpringBoot. It is a hot-swappable annotation for Feign clients to provide load balancing, through which you can dynamically proxy to create Feign clients.
The simple understanding is the core of the internal communication between the distributed architecture services and the sub-module systems.
It is generally used when one system calls the interface of another system, as follows:
Notes
@ FeignClient ("XXX") public interface XX {....}
This annotation is generally created in the interface interface, and then used in the business class @ Autowired is very simple and convenient.
Problem background
After creating the interface interface, of course, you need to define the interface method that calls the service. This method corresponds to the controller interface of this FeignClient, and you must override the interface method (return object, parameter values are exactly the same).
When the following error occurs in the startup project, when I think that the API method is called in the business class, the parameter passed is empty null and an error is reported.
FactoryBean threw exception on object creation; nested exception is
Java.lang.IllegalStateException: RequestParam.value () was empty on parameter 0
When rebooting when replacing the pass parameter with data; still report the error as above.
Post a full screenshot of the error report.
Org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountRecordController': Unsatisfied dependency expressed through field' withdrawCountService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountServiceImpl': Unsatisfied dependency expressed through field' cumClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epaylinks.efps.pas.clr.client.CumClient': FactoryBean threw exception on object creation Nested exception is java.lang.IllegalStateException: RequestParam.value () was empty on parameter 0 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject (AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject (InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues (AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean (AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory. Java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject (AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:197)
Solution.
In the @ FeignClien ("XX") interface class, when you check the parameter definition for each method:
Are there any of the following situations?
@ RequestMapping (value= "/ XXX/query", method = RequestMethod.GET) public PageResult query (@ RequestParam (required = false) String XXCode, @ RequestParam (value= "XXnName", required = false) String institutionName, @ RequestParam (value= "startTime", required = false) String startTime
Here's the problem:
@ RequestParam (required = false) String XXCode
This parameter is missing a value = "XXCode". After version 4.0 of Spring, the @ RequestParam annotation has a good encapsulation feature for parameter passing and is strictly checked.
Change to:
@ RequestParam (value = "XXCode", required = false) String XXCode
After that, the problem is solved perfectly; the restart project is normal.
Interject: when you call the same @ FeignClien ("XX") project in multiple places in a project, creating an interface in multiple packages does not affect it.
After reading the above, do you have any further understanding of the contextId attribute in the @ FeignClient annotation in Spring Cloud? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.