WeGa
WeGa

Reputation: 890

How to generate rest client api for Spring Boot microservice from source?

Is there a way to configure Spring Boot application to produce (for example, through a gradle task) a jar-file with rest client for it?

I heard of Swagger and libraries like springdoc, springfox, but that generates web api upon application startup. And I want to automate the process of communication between microservices inside Kubernetes cluster with managing rest api clients with CI/CD instead of manual work.

Upvotes: 0

Views: 1374

Answers (1)

Sree Kumar
Sree Kumar

Reputation: 2245

Have you tried OpenFeign? Within SpringMVC, we can use SpringMVC's @RequestMapping and other annotations to generate a client to the API that these annotations are pointing to. Please refer the documentation here.

Now, since you want this to be automated, you may try this:

  • Write an annotation processor that can process @Controller or whichever flavour you are using.
  • This processor will generate the interface that SpringBoot needs as the Feign configuration
  • The processor will generate exactly the same methods as in the controller class with the same annotations (you may need to add/remove some parameters to the SpringMVC annotations)
  • This, then should be given as a Gradle annotationProcessor dependency

Of course, now your SpringBoot application needs @EnableFeignClients and the corresponding dependencies.

Hope this works for you.

Upvotes: 1

Related Questions