AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36001

How to create a java client for a Spring service?

I have a service definition using Spring annotations. Example (source):

@RequestMapping(value = "/ex/foos/{id}", method = GET)
@ResponseBody
public String getFoosBySimplePathWithPathVariable(
  @PathVariable("id") long id) {
    return "Get a specific Foo with id=" + id;
}

The question is whether spring (or another library) can auto-create a remote implementation (client) of the same API without the need to manually type paths, method type, param names, etc. (like needed when using RestTemplate)?

Example of an such a client usage:

FooClient fooClient = new FooClient("http://localhost:8080");
String foo = fooClient.getFoosBySimplePathWithPathVariable(3l);

How can I get to such a client "generated" implementation"?

Upvotes: 6

Views: 955

Answers (2)

Everv0id
Everv0id

Reputation: 1980

You are probably looking for Feign Client. It does everything you need: calling one service via HTTP is similar to calling method of Java interface. But to make it work you need Spring Cloud, standard Spring framework doesn't have this feature yet.

Upvotes: 1

TheOni
TheOni

Reputation: 820

You can generate it using Swagger Editor. You shoud just define the path of the resources and then it'll generate for you the client for almost any language of your choice

Upvotes: 0

Related Questions