Skip to content

Dynamically create Spring Rest (proxy class) client from annotated interface.

License

Notifications You must be signed in to change notification settings

tomasbjerre/spring-rest-client

Repository files navigation

Spring Rest Client

Maven Central

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.

About

Dynamically create Spring Rest (proxy class) client from annotated interface.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages