How to generate Java client proxy for RESTful service implemented with Spring?

Dima picture Dima · May 12, 2013 · Viewed 14.7k times · Source

We use Spring to implement REST controller, for example:

@Controller
@RequestMapping("/myservice") 
public class MyController {
    @RequestMapping(value = "foo", method = RequestMethod.GET)
    public @ResponseBody string foo() {...}
}

I can call this service using spring RestTemplate, and it works fine, but I would prefer to invoke it using a proxy, instead of typeless invocation using string url:

// client code:
MyController proxy = getProxy("baseUrl", MyController.class);
String results = proxy.foo();

So the input to proxy generation is java interface with annotations describing REST details. I read this article and it looks like all types of remote calls do have proxies, and all I need for REST is something like RestProxyFactoryBean, that would take my REST java interface and return type-safe proxy that uses RestTemplate as implementation.

The closest solution I found is JBoss RESTEasy.

But it seems to use different set of annotations, so I am not sure it will work with annotations I already have: @Controller, @RequestMapping. Are there other options, or RESTEasy is the only one? Note, I am spring newbie so some obvious spring things are pretty new to me.

Thank you.
Dima

Answer

pcan picture pcan · Feb 18, 2016

You can try Feign by Netflix, a lightweight proxy-based REST client. It works declaratively through annotations, and it's used by Spring Cloud projects to interact with Netflix Eureka.