blob: 7bdd9fd75fd239acd43fa8488d5bf7bb27649658 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<link type="text/css" rel="stylesheet" href="/resources/site.css">
<script src='/resources/space.js'></script>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="keywords" content="business integration, EAI, SOA, Service Oriented Architecture, web services, SOAP, JBI, JMS, WSDL, XML, EDI, Electronic Data Interchange, standards support, integration standards, application integration, middleware, software, solutions, services, CXF, open source">
<meta name="description" content="Apache CXF, Services Framework - JAXRSClientSpringBoot">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shCoreCXF.css">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shThemeCXF.css">
<script src='/resources/highlighter/scripts/shCore.js'></script>
<script src='/resources/highlighter/scripts/shBrushXml.js'></script>
<script src='/resources/highlighter/scripts/shBrushJava.js'></script>
<script>
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
</script>
<title>
Apache CXF -- JAXRSClientSpringBoot
</title>
</head>
<body onload="init()">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td id="cell-0-0" colspan="2">&nbsp;</td>
<td id="cell-0-1">&nbsp;</td>
<td id="cell-0-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-1-0">&nbsp;</td>
<td id="cell-1-1">&nbsp;</td>
<td id="cell-1-2">
<!-- Banner -->
<div class="banner" id="banner"><div><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="left" colspan="1" nowrap>
<a shape="rect" href="http://cxf.apache.org/" title="Apache CXF"><span style="font-weight: bold; font-size: 170%; color: white">Apache CXF</span></a>
</td><td align="right" colspan="1" nowrap>
<a shape="rect" href="http://www.apache.org/" title="The Apache Sofware Foundation"><img border="0" alt="ASF Logo" src="http://cxf.apache.org/images/asf-logo.png"></a>
</td></tr></table></div></div>
<!-- Banner -->
<div id="top-menu">
<table border="0" cellpadding="1" cellspacing="0" width="100%">
<tr>
<td>
<div align="left">
<!-- Breadcrumbs -->
<a href="index.html">Index</a>&nbsp;&gt;&nbsp;<a href="restful-services.html">RESTful Services</a>&nbsp;&gt;&nbsp;<a href="jax-rs.html">JAX-RS</a>&nbsp;&gt;&nbsp;<a href="jaxrs-services-configuration.html">JAXRS Services Configuration</a>&nbsp;&gt;&nbsp;<a href="jaxrsclientspringboot.html">JAXRSClientSpringBoot</a>
<!-- Breadcrumbs -->
</div>
</td>
<td>
<div align="right">
<!-- Quicklinks -->
<div id="quicklinks"><p><a shape="rect" href="http://cxf.apache.org/download.html">Download</a> | <a shape="rect" href="http://cxf.apache.org/docs/index.html">Documentation</a></p></div>
<!-- Quicklinks -->
</div>
</td>
</tr>
</table>
</div>
</td>
<td id="cell-1-3">&nbsp;</td>
<td id="cell-1-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-2-0" colspan="2">&nbsp;</td>
<td id="cell-2-1">
<table>
<tr valign="top">
<td height="100%">
<div id="wrapper-menu-page-right">
<div id="wrapper-menu-page-top">
<div id="wrapper-menu-page-bottom">
<div id="menu-page">
<!-- NavigationBar -->
<div id="navigation"><ul class="alternate"><li><a shape="rect" href="overview.html">Overview</a></li><li><a shape="rect" href="how-tos.html">How-Tos</a></li><li><a shape="rect" href="frontends.html">Frontends</a></li><li><a shape="rect" href="databindings.html">DataBindings</a></li><li><a shape="rect" href="transports.html">Transports</a></li><li><a shape="rect" href="configuration.html">Configuration</a></li><li><a shape="rect" href="debugging-and-logging.html">Debugging and Logging</a></li><li><a shape="rect" href="tools.html">Tools</a></li><li><a shape="rect" href="restful-services.html">RESTful Services</a></li><li><a shape="rect" href="wsdl-bindings.html">WSDL Bindings</a></li><li><a shape="rect" href="service-routing.html">Service Routing</a></li><li><a shape="rect" href="dynamic-languages.html">Dynamic Languages</a></li><li><a shape="rect" href="ws-support.html">WS-* Support</a></li><li><a shape="rect" href="advanced-integration.html">Advanced Integration</a></li><li><a shape="rect" href="deployment.html">Deployment</a></li><li><a shape="rect" href="schemas-and-namespaces.html">Use of Schemas and Namespaces</a></li></ul><hr><ul class="alternate"><li><p>Search</p></li></ul><form enctype="application/x-www-form-urlencoded" method="get" id="cse-search-box" action="http://www.google.com/cse">
<div>
<input type="hidden" name="cx" value="002890367768291051730:o99qiwa09y4">
<input type="hidden" name="ie" value="UTF-8">
<input type="text" name="q" size="21">
<input type="submit" name="sa" value="Search">
</div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script><hr><ul class="alternate"><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest/">API 3.2.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest-3.1.x/">API 3.1.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/">CXF Website</a></li></ul><p>&#160;</p><p><a shape="rect" class="external-link" href="http://www.apache.org/events/current-event.html"><span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image confluence-external-resource" src="http://www.apache.org/events/current-event-125x125.png" data-image-src="http://www.apache.org/events/current-event-125x125.png"></span></a></p></div>
<!-- NavigationBar -->
</div>
</div>
</div>
</div>
</td>
<td height="100%">
<!-- Content -->
<div class="wiki-content">
<div id="ConfluenceContent"><p><style type="text/css">/*<![CDATA[*/
div.rbtoc1636141594537 {padding: 0px;}
div.rbtoc1636141594537 ul {list-style: disc;margin-left: 0px;}
div.rbtoc1636141594537 li {margin-left: 0px;padding-left: 0px;}
/*]]>*/</style></p><div class="toc-macro rbtoc1636141594537">
<ul class="toc-indentation"><li><a shape="rect" href="#JAXRSClientSpringBoot-Introduction">Introduction</a></li><li><a shape="rect" href="#JAXRSClientSpringBoot-Setup">Setup</a></li><li><a shape="rect" href="#JAXRSClientSpringBoot-EnablingWebClients">Enabling WebClients</a></li><li><a shape="rect" href="#JAXRSClientSpringBoot-EnablingProxyClients">Enabling ProxyClients</a></li><li><a shape="rect" href="#JAXRSClientSpringBoot-DiscoveryofServiceEndpoints">Discovery of Service Endpoints</a></li><li><a shape="rect" href="#JAXRSClientSpringBoot-Configuration">Configuration</a></li></ul>
</div><h1 id="JAXRSClientSpringBoot-Introduction">Introduction</h1><p>This page describes how <a shape="rect" href="jax-rs-client-api.html">CXF JAX-RS Client</a> code can be used inside SpringBoot applications.</p><p>Please see a&#160;<a shape="rect" href="http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarter">CXF JAX-RS starter</a> section on how to enable JAX-RS endpoints.</p><h1 id="JAXRSClientSpringBoot-Setup">Setup</h1><p>If your SpringBoot Application depends on a <a shape="rect" href="http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarter">CXF JAX-RS starter</a> then no more dependencies are required.</p><p>If you'd like to run JAX-RS clients in a pure client-side&#160;SpringBoot Application then the following Maven pom should suffice in many cases:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;1.4.5.RELEASE&lt;/version&gt;
&lt;/parent&gt;
&lt;artifactId&gt;spring-boot-cxf-client-application&lt;/artifactId&gt;
&lt;groupId&gt;org.apache.cxf.samples&lt;/groupId&gt;
&lt;version&gt;3.1.11&lt;/version&gt;
&lt;name&gt;Spring Boot CXF Client Application&lt;/name&gt;
&lt;description&gt;Spring Boot CXF Client Application&lt;/description&gt;
&lt;properties&gt;
&lt;cxf.version&gt;3.1.11&lt;/cxf.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.cxf&lt;/groupId&gt;
&lt;artifactId&gt;cxf-rt-rs-client&lt;/artifactId&gt;
&lt;version&gt;${cxf.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;mainClass&gt;sample.rs.client.SpringBootClientApplication&lt;/mainClass&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;
</pre>
</div></div><h1 id="JAXRSClientSpringBoot-EnablingWebClients">Enabling WebClients</h1><p><a shape="rect" href="http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-CXFWebClientAPI">WebClient</a> can be auto-wired with the help of <strong>EnableJaxRsWebClient</strong> annotation.</p><p>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.</p><p>&#160;</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">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));
}
};
}
}</pre>
</div></div><p>&#160;</p><h1 id="JAXRSClientSpringBoot-EnablingProxyClients">Enabling ProxyClients</h1><p><a shape="rect" href="http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-Proxy-basedAPI">Proxy Clients</a> can be auto-wired with the help of <strong>EnableJaxRsProxyClient</strong> annotation.</p><p>It creates a proxy from the auto-discovered service class interface.</p><p>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.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">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"));
}
};
}
}
</pre>
</div></div><p>&#160;</p><p>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:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">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&lt;?&gt; getServiceClass() {
return HelloService.class;
}
}
}</pre>
</div></div><h1 id="JAXRSClientSpringBoot-DiscoveryofServiceEndpoints">Discovery of Service Endpoints</h1><p>Discovery of JAX-RS endpoint addresses published to a well-known service registries such as Netflix Eureka Registry is shown in a <a shape="rect" class="external-link" href="https://github.com/apache/cxf/tree/master/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application" rel="nofollow">JAX-RS Spring Boot Scan</a> demo.</p><p>This is achieved with the help of CXF Failover and/or LoadBalancing features.</p><h1 id="JAXRSClientSpringBoot-Configuration">Configuration</h1><p>The configuration properties apply to both WebClient and Proxy configurations.</p><p><strong>cxf.jaxrs.client.address</strong> is a required property which identifies a target address.</p><p><strong>cxf.jaxrs.client.thread-safe</strong> property can be set to true to make the clients thread-safe.</p><p><strong>cxf.jaxrs.client.headers.accept</strong> property can be used to set HTTP Accept header.</p><p><strong>cxf.jaxrs.client.headers.content-type</strong> property can be used to set HTTP Content-Type header.</p><p><strong>cxf.jaxrs.client.classes-scan-packages</strong> property can be used to auto-discover JAX-RS service class interfaces (for proxies) and providers for proxies and web clients.</p></div>
</div>
<!-- Content -->
</td>
</tr>
</table>
</td>
<td id="cell-2-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-3-0">&nbsp;</td>
<td id="cell-3-1">&nbsp;</td>
<td id="cell-3-2">
<div id="footer">
<!-- Footer -->
<div id="site-footer">
<a href="http://cxf.apache.org/privacy-policy.html">Privacy Policy</a> -
(<a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=65145466">edit page</a>)
(<a href="https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=65145466&amp;showComments=true&amp;showCommentArea=true#addcomment">add comment</a>)<br>
Apache CXF, CXF, Apache, the Apache feather logo are trademarks of The Apache Software Foundation.<br>
All other marks mentioned may be trademarks or registered trademarks of their respective owners.
</div>
<!-- Footer -->
</div>
</td>
<td id="cell-3-3">&nbsp;</td>
<td id="cell-3-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-4-0" colspan="2">&nbsp;</td>
<td id="cell-4-1">&nbsp;</td>
<td id="cell-4-2" colspan="2">&nbsp;</td>
</tr>
</table>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-4458903-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>