How to inject Feign Client with out using Spring Boot and call a REST Endpoint

Viewed 2791

I have two Java processes - which get spawned from the same Jar using different run configurations

Process A - Client UI component , Developed Using Spring bean xml based approach. No Spring Boot is there.

Process B - A new Springboot Based component , hosts REST End points.

Now from Process A , on various button click how can I call the REST end points on Process B using Feign Client.

Note - Since Process A is Spring XML based , right at the moment we can not convert that to Spring boot. Hence @EnableFeignClients can not be used to initialise the Feign Clients

So Two questions

1) If the above is possible how to do it ?

2) Till Process A is moved to Spring boot - is Feign still an easier option than spring REST template ?

2 Answers

Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSockets and you can easily use feign without spring boot. And Yes, feign still better option to use because Feign Simplify the HTTP API Clients using declarative way as Spring REST does.

1) Define http methods and endpoints in interface.

@Headers({"Content-Type: application/json"})
public interface NotificationClient {

    @RequestLine("POST")
    String notify(URI uri, @HeaderMap Map<String, Object> headers, NotificationBody body);
}

2) Create Feign client using Feign.builder() method.

Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(customDecoder())
                .target(Target.EmptyTarget.create(NotificationClient.class));

There are various decoders available in feign to simplify your tasks.

You are able to just initialise Feign in any code (without spring) just like in the readme example:

public static void main(String... args) {
    GitHub github = Feign.builder()
                     .decoder(new GsonDecoder())
                     .target(GitHub.class, "https://api.github.com");
...
}

Please take a look at the getting started guide: feign on github

Related