TL;DR: auto generate HTTP client for each REST Spring Controller to reuse in other services written with Spring.
When working with multiple micro-services that are written with spring I find myself rewriting the client for each controller. Lets say I write a controller in service X:
@RestController
public class SubscriptionController {
@Autowired
private SubscriptionService subscriptionService;
@RequestMapping(value = "/subscription", method = RequestMethod.GET)
public SubscriptionDTO getMySubscription() {
return subscriptionService.getCurrentUserSubscription();
}
}
I will import the DTO in service Y, write HTTP request to the mapping defined in the other service, and write tests for it.
@Service
public class SubscriptionApiService {
@Autowired
private HttpClient httpClinet;
public SubscriptionDTO getMySubscription() {
return httpClient.get("/subscription", SubscriptionDTO.class);
}
}
It's very a repetitive process, and I'm quite sure someone already wrote a library to automate this process. However I was unable to find anything like that. Any idea?
You can achieve this with Swagger2
@SpringBootApplication
@EnableSwagger2
public class Application extends SpringApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
You can access Swagger2
html by this url: localhost:8080/swagger-ui.html
I also @ComponentScan
my packages and these are the dependencies
needed for Swagger2
:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With