Example Of Simple REST Client Using CXF WebClient API

Vafion is a  Preferred Vacation Rental Technology Partner. We focus on travel and more specifically on vacation rental suppliers, integration of property management systems, OTA channel integrations etc. API Integrations is a thing which we do every day, so Apache CXF is the main tool we handle these days. Simple REST Client using CXF WebClient API.

Vacation Rental Inida Image vacation rental API Channel OTA Integrations technology services solutions India property management system India vacation rental technology solutions

Here I will show a simple REST API access using Apache CXF WebClientAPI.

In order to test the API, I have created a Spring Boot REST service exposing a Message State @ “http://localhost:8080/test” . The following image will show the service accessed via Postman.

cxf-test-api-vacation rental API Channel OTA Integrations technology services solutions India property management system India vacation rental technology solutions
cxf-test-api

 

In order to bind the JSON to a POJO we can either directly create one using some tools like Jsonschema2pojo or can write it by hand. I have created one by hand (I also haven’t bothered to annotate the classes with JSON annotations, the rule of thumb here is that make the field names equals to the json node names).

Message Class

[code language=”java”]
public class Message{

private String id;
private String body;

public Message() {
}
public Message(String id, String body) {
this.id = id;
this.body = body;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
return “Message [id=” + id + “, body=” + body + “]”;
}

}
[/code]

We are using the Apache CXF 2.4.2 and the maven dependencies are

[code language=”xml”]
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.4.2</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.0.2</version>
</dependency>
[/code]

I’m using Jackson to process the JSON message, thats why I have added the Jackson Dependency.
Now the sample code using the WebClient API to access the REST Service.

[code language=”java”]
public class SimpleCXFClient {

public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
WebClient client = WebClient
.create(“http://localhost:8080/”,
Collections.singletonList(new JacksonJsonProvider()))
.path(“test”).accept(MediaType.APPLICATION_JSON_TYPE);
Message message = client.get(Message.class);
System.out.println(“Message recieved : ” + message);
}

}
[/code]

In the above code you can see that we are passing an instance of JacksonJsonProvider, the purpose of the Object is to process the JSON and bind it to the whichever class we are specifying while making the request.

We can also make use of the Spring framework coming bundled with the CXF to externalize configurations and making use of the power of DI.

Spring Configuration. spring-cxf-client.xml

[code language=”xml”]
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:util=”http://www.springframework.org/schema/util”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd”>

<bean id=”jacksonJsonProvider” class=”com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider”></bean>

<util:list id=”webClientProviders”>
<ref bean=”jacksonJsonProvider”/>
</util:list>

<bean id=”jsonWebClient” class=”org.apache.cxf.jaxrs.client.WebClient”
factory-method=”create”>
<constructor-arg type=”java.lang.String” value=”http://localhost:8080/”/>
<constructor-arg ref=”webClientProviders” />
</bean>

</beans>
[/code]

Another Client using Spring

[code language=”java”]
public class SimpleCXFClientUsingSpring {

public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext(“spring-*.xml”);
WebClient client = context.getBean(“jsonWebClient”, WebClient.class);
client.path(“test”).accept(MediaType.APPLICATION_JSON_TYPE);
Message message = client.get(Message.class);
System.out.println(“Message recieved : ” + message);
}
}
[/code]

Output:

vacation rental API Channel OTA Integrations technology services solutions India property management system India vacation rental technology solutions
console-out-cxf1

SimpleCXFClient

vacation rental API Channel OTA Integrations technology services solutions India property management system India vacation rental technology solutions
console-out-cxf2-spring

SimpleCXFClientUsingSpring

Please write to info@vafion.com  if you need to know more about us.

A blog from Vafion – A preferred vacation rental technology partner

Follow us on  Linkedin

Visit Vafion | Facebook | Linkedin

Similar Posts:

Related Posts

Stay UpdatedSubscribe and Get the latest updates from Vafion