Dynamically create Spring (proxy class) client from annotated interface. Very lightweight, only depends on spring-web:4.3
, compared to alternatives like Feign.
Given MyApiInterface
is a Spring-annotated Java interface.
final MyApiInterface myClient = SpringRestClientBuilder
.create(MyApiInterface.class)
.setUrl(this.getMockUrl())
.setRestTemplate(restTemplate) // Optional
.setHeader("header-name", "the value") // Optional
.setHeaders(HttpHeaders) // Optional
.build();
Here myClient
is a dynamically created Java Proxy object.
final ResponseEntity<MyDTO> response = myClient.getMyDto();
The method invocation (getMyDto()
) on that object (myClient
) will be translated to a HTTP request using the annotations of that method in that interface. And response
will be the result of that HTTP request.
Supports both signatures:
ResponseEntity<X> deleteOrder(Long)
X deleteOrder(Long)
See also test cases.