...
Span | ||
---|---|---|
| ||
JAX-RS : Client API |
...
Table of Contents |
---|
Maven Dependency
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.0-milestone1<15</version> </dependency> |
In CXF 2.7.x no JAX-RS 2.0 Client API is supported and CXF specific Client API is located in the cxf-rt-frontend-jaxrs module.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-hc</artifactId> <!-- 2.7.8 or 3.0.0-milestone115 --> <version>${cxf.version}</version> </dependency> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
@Path("/bookstore") public interface BookStore { @GET Books getAllBooks(); @Path("{id}") BookResource getBookSubresource(@PathParam("id") long id) throws NoBookFoundException; } public class BookStoreImpl implements BookStore { public Books getAllBooks() {} public BookBookResource getBookSubresource(long id) throws NoBookFoundException {} } public interface BookResource { @GET Book getBook(); } public class BookResourceImpl implements BookResource { Book getBook() {} } |
...
For injecting proxies via a spring context, use the jaxrs:client element like:
Code Block | ||||
---|---|---|---|---|
| ||||
<jaxrs:client id="restClient" address="http://localhost:${testutil.ports.BookServerRestSoap}/test/services/rest" serviceClass="org.apache.cxf.systest.jaxrs.BookStoreJaxrsJaxws" inheritHeaders="true"> <jaxrs:headers> <entry key="Accept" value="text/xml"/> </jaxrs:headers> </jaxrs:client> |
See this bean for a full example of how jaxrs:client can be used to inject a proxy. Note that WebClient can also be injected as a jaxrs:client.
Asynchronous proxy invocations
...
Proxy sub-resource methods returning Objects can not be invoked. Prefer to have sub-resource methods returning typed classes: interfaces, abstract classes or concrete implementations.
Working with user models
Proxies can be created with the external user model being applied to a proxy class, for example:
...
The above code will send requests like "GET http://books/1", "GET http://books/2", etc.
If the request URI can be parameterized then you may want to use the following code:
...
Code Block | ||||
---|---|---|---|---|
| ||||
<bean id="myJsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider" > <property name="supportUnwrapped" value="true" /> <property name="wrapperName" value="nodeName" /> </bean> <util:list id="webClientProviders"> <ref bean="myJsonProvider"/> </util:list> <bean id="myWebClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create"> <constructor-arg type="java.lang.String" value="http://some.base.url.that.responds/" /> <constructor-arg ref="webClientProviders" /> </bean> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<jaxrs:client id="webClient" address="https://localhost:${port}/services/rest" serviceClass="org.apache.cxf.jaxrs.client.WebClient"> <jaxrs:headers> <entry key="Accept" value="text/xml"/> </jaxrs:headers> </jaxrs:client> |
The only limitation of using this option is that some of jaxrs:client attributes ("inheritHeaders", "modelRef") and elements ("model") are not really applicable to WebClient.
...
A single client doing multiple invocations without changing the current URI or headers is thread-safe (while creating a Invocation.Builder instances concurrently is not thread-safe since the shared instance of non-thread-safe class ClientProviderFactory is used under the hood). The only limitation in this case applies to proxies, in that they can not get "out of band" headers without synchronizing, ex :
...
When injecting clients from Spring, one can add 'username' and 'password' values as attributes to jaxrs:client elements or add them to WebClient factory create methods.
Clients in Spring Boot
Please see JAXRSClientSpringBoot documentation on how CXF JAX-RS Clients can be used in a SpringBoot Application.
Clients and HTTP(s)
The default HttpClientHTTPConduit conduit by default supports the following HTTPS protocols: TLSv1, TLSv1.1, TLSv1.2, TLSv1.3. Since Apache CXF 4.0.4 / 3.6.3 release, the default HttpClientHTTPConduit respects https.protocols system property (see please https://blogs.oracle.com/java/post/diagnosing-tls-ssl-and-https) and if set, would use the provided protocols. This behavior could be turned off by setting https.protocols.ignored system property to "true" (the default value is "false").