Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Spring Cloud Feign client share interface with an Spring Web Controller?

Building an endpoint and client with Spring MVC and Feign Client (with spring cloud). I had the thought that since both ends need to have the same annotations - and that they have to be pretty much in sync. Maybe I could define them in an interface and have the two ends implement that.

Testing it out I was somewhat surprised that it actually works for the Spring Web end.

But it I cannot find a way to do the same for a Feign client.

I basically have the interface:

@RequestMapping("/somebaseurl")
public interface ServiceInterface {
  @RequestMapping(value = "/resource/{identifier}", method = RequestMethod.POST)
  public SomeResource getResourceByIdentifier(String identifier);
}

And then the RestController

@RestController
public class ServiceController implements ServiceInterface {
    public SomeResource getResourceByIdentifier(@PathVariable("identifier") String identifier) {
    // Do some stuff that gets the resource
        return new SomeResource();
    }
}

And then finally the Feign Client

@FeignClient("serviceName")
public interface ServiceClient extends ServiceInterface {
}

The Feign client seems to not read the inherited annotations. So is there some other way I can accomplish the same thing? Where I can make the ServiceInterface into Feign client without annotating it directly?

like image 286
Kristoffer Avatar asked Mar 26 '15 17:03

Kristoffer


People also ask

Can we use feign client without spring boot?

Yes, it's possible. You can find the documentation on their Github page. Feign wasn't integrated with Spring initially.

What is the difference between RestTemplate and feign client?

When we use the RestTemplate to call the RESTful service, it creates duplication of code that talks to RESTful services. When we define Feign, we need only to define a proxy and define a single method into it. Feign helps us to simplify client code to talk to the RESTful web services.

Is feign client synchronous?

The API that we defined via Feign is synchronous — meaning that it makes blocking calls to the server. Feign allows us to easily define async APIs as well — utilizing CompletableFutures under the hood.

How feign client works internally?

A central concept in Spring Cloud's Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer using the @FeignClient annotation.


1 Answers

This is possible as of Feign 8.6.0. From the Spring Cloud docs:

Feign Inheritance Support

Feign supports boilerplate apis via single-inheritance interfaces. This allows grouping common operations into convenient base interfaces. Together with Spring MVC you can share the same contract for your REST endpoint and Feign client.

UserService.java

public interface UserService {

    @RequestMapping(method = RequestMethod.GET, value ="/users/{id}")
    User getUser(@PathVariable("id") long id);
}

UserResource.java

@RestController
public class UserResource implements UserService {

}

UserClient.java

@FeignClient("users")
public interface UserClient extends UserService {

}
like image 167
Alex Wittig Avatar answered Oct 12 '22 16:10

Alex Wittig