About the problem that requestmapping on feignclient cannot take effect

Time:2021-11-24

problem
I have two feignclients that inherit an interface. The two clients have different URL implementations. One of them needs to add an effect similar to @ requestmapping on the class, because @ requestmapping is effective on the method in feignclient. Can it be implemented on the class like @ controller, After trying, it is found that @ requestmapping and @ feignclient will not take effect when they work together on a class.

reason
From the source code, we can get that during feignclient injection, the bean is initialized through feignclientfactorybean, in which requestmapping is not processed.

Solving process
Tip 1: feignclient is an RPC framework that requests through HTTP protocol and is encapsulated with resttemplate, so there must be encapsulation of URL. I just need to find out if I can add an address when encapsulating the URL.

Tip 2: during dependency injection, springfactory will find the corresponding bean information through dogetbean() method. If the instance is a beanfactory, it will get the object through beanfactory. Getobject(). Then I just need to enter feignclienfactorybean to view the corresponding methods.

The corresponding processing code found in the source code is as follows:

this.url = this.url + this.cleanPath();

private String cleanPath() {
    String path = this.path.trim();
    if (StringUtils.hasLength(path)) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
    }
        return path;
}

In the above code, it can be found that the effect I want can be achieved by setting the path attribute.

result
I can achieve the desired effect by setting the path attribute in @ feignclient. @FeignClient(name="serviceName", url = "http://localhost:8080", path="value")

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]