...
Code Block | ||
---|---|---|
| ||
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> </parent> <artifactId>spring-boot-cxf-client-application</artifactId> <groupId>org.apache.cxf.samples</groupId> <version>3.1.7</version> <name>Spring Boot CXF Client Application</name> <description>Spring Boot CXF Client Application</description> <properties> <cxf.version>3.1.7</cxf.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>${cxf.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>sample.rs.client.SpringBootClientApplication</mainClass> </configuration> </plugin> </plugins> </build> </project> |
Enabling WebClients
WebClient can be auto-wired with the help of EnableJaxRsWebClient annotation.
JAX-RS providers (annotated with @Provider) and marked as Spring Components are added to WebClient. The providers which are not marked as Spring Components can also be optionally auto-discovered. WebClient can also be configured with optional headers such as Accept and Content-Type and made thread-safe.
Code Block | ||
---|---|---|
| ||
package sample.rs.client; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.jaxrs.client.spring.EnableJaxRsWebClient; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableJaxRsWebClient public class SpringBootClientApplication { public static void main(String[] args) { SpringApplication.run(SpringBootClientApplication.class, args); } @Bean CommandLineRunner initWebClientRunner(final WebClient webClient) { return new CommandLineRunner() { @Override public void run(String... runArgs) throws Exception { System.out.println(webClient.path("sayHello/ApacheCxfWebClientUser").get(String.class)); } }; } } |
Configuration
...
Enabling ProxyClients
Proxy Clients can be auto-wired with the help of EnableJaxRsProxyClient annotation.
It creates a proxy from the auto-discovered service class interface.
JAX-RS providers (annotated with @Provider) and marked as Spring Components are added to proxy clients. The providers which are not marked as Spring Components can also be optionally auto-discovered. Proxy can also be configured with optional headers such as Accept and Content-Type (if JAX-RS @Produces and/or @Consumes are missing or need to be overridden) and made thread-safe.
Code Block | ||
---|---|---|
| ||
package sample.rs.client;
import org.apache.cxf.jaxrs.client.spring.EnableJaxRsProxyClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import sample.rs.service.HelloService;
@SpringBootApplication
@EnableJaxRsProxyClient
public class SpringBootClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootClientApplication.class, args);
}
@Bean
CommandLineRunner initProxyClientRunner(final HelloService client) {
return new CommandLineRunner() {
@Override
public void run(String... runArgs) throws Exception {
System.out.println(client.sayHello("ApacheCxfProxyUser"));
}
};
}
}
|
If you prefer to specify a proxy service interface directly in the client code you can drop EnableJaxRsProxyClient annotation and provide a simple JaxRsProxyClientConfiguration extension instead:
Code Block | ||
---|---|---|
| ||
package sample.rs.client; import org.apache.cxf.jaxrs.client.spring.JaxRsProxyClientConfiguration; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import sample.rs.service.HelloService; @SpringBootApplication public class SpringBootClientApplication { public static void main(String[] args) { SpringApplication.run(SpringBootClientApplication.class, args); } @Bean CommandLineRunner initProxyClientRunner(final HelloService client) { return new CommandLineRunner() { @Override public void run(String... runArgs) throws Exception { System.out.println(client.sayHello("ApacheCxfProxyUser")); } }; } @Configuration static class HeloServiceConfiguration extends JaxRsProxyClientConfiguration { @Override protected Class<?> getServiceClass() { return HelloService.class; } } } |
Configuration
The configuration properties apply to both WebClient and Proxy configurations.
cxf.jaxrs.client.address is a required property which identifies a target address.
cxf.jaxrs.client.thread-safe property can be set to true to make the clients thread-safe.
cxf.jaxrs.client.headers.accept property can be used to set HTTP Accept header.
cxf.jaxrs.client.headers.content-type property can be used to set HTTP Content-Type header.
cxf.jaxrs.client.classes-scan-packages property can be used to auto-discover JAX-RS service class interfaces (for proxies) and providers for proxies and web clients.