Common ReST Client Library
This project provides a library for Spring and POJO-based REST client instantiation. It includes special classes with classifiers for two popular JAXRS-based client frameworks: Apache CXF and Jersey.
Usage
First, you will need to include the library in your project.
<project>
...
<dependencies>
...
<dependency>
<groupId>com.inteligr8</groupId>
<artifactId>common-rest-client</artifactId>
<classifier>...</classifier>
<version>...</version>
</dependency>
...
</dependencies>
...
</project>
Valid classifier
values are cxf
or jersey
.
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.cxf") // 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 ClientCxfConfiguration
) interface.
@Configuration
public class ... implements ClientCxfConfiguration {
...
}
For Jersey implementations, just use client.jersey
and ClientJerseyConfiguration
. If you want to provide one of each, then follow the instructions for multiple clients below.
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 ClientCxfImpl {
@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();
...
ClientCxfImpl client = new ClientCxfImpl(config);
MyJaxRsApi api = client.getApi(MyJaxRsApi.class);