Common ReST Client Library
This project provides a library for Spring and POJO-based REST client instantiation. It includes special classes for the Jersey JAXRS-based client frameworks.
Usage
First, you will need to include the library in your project.
<project>
...
<dependencies>
...
<dependency>
<groupId>com.inteligr8</groupId>
<artifactId>common-rest-client</artifactId>
<version>...-jersey</version>
</dependency>
...
</dependencies>
...
</project>
Spring Framework
Single Client
If you will only be declaring a single client in your Spring context, this is easy. You will just need two things. First, inject the single client into any of your Spring beans. You may do inject it into more than one. An example is below:
@Component
public class ... {
@Autowired
@Qualifier("client.jersey") // may be optional
private Client client;
}
Next, you need to configure that client. You can do that by providing a single implementation of the ClientConfiguration
(or ClientJerseyConfiguration
) interface.
@Configuration
public class ... implements ClientJerseyConfiguration {
...
}
Multiple Clients
If you will or may have multiple clients in your Spring context, there is an extra step. You will still need to define a ClientConfiguration
for each. On top of that, you will need to create specialized implementations of each client. That special implementation will reference the configuration directly. An example is below.
@Component("my.client")
public class MyClient extends ClientJerseyImpl {
@Autowired
public MyClient(MyClientConfiguration config) {
super(config);
}
}
You can then inject your client(s) into your Spring beans. Like the example below:
@Component
public class ... {
@Autowired
private MyClient client;
@PostConstruct
public void init() {
MyJaxRsApi api = this.client.getApi(MyJaxRsApi.class);
}
}
### POJO
You do not have to use the Spring framework to use these classes. You can instantiate them directly. But you wil still need to create a `ClientConfiguration` as mentioned above.
```java
MyClientConfiguration config = new MyClientConfiguration();
...
ClientJerseyImpl client = new ClientJerseyImpl(config);
MyJaxRsApi api = client.getApi(MyJaxRsApi.class);