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.
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.
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:
SimpleCXFClient
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:
- Knowing the SparkJava Framework
- HOW vREST API CAN BE USED FOR REST TESTING
- Accelerate your API integrations in hospitality industry
- What is Cross-site request forgery or CSRF/XSRF ?
- How to reach your potential guests using optimized long tail keywords
- How supplier API integrations are done in travel booking platforms?
- Make your rentals standout! Display Visual and Engaging Videos with YouTube
- Advantages of working with OTA Channels
- Vacation Rentals Channel Marketing : How to sell more?
- What Is Expedia Virtual Card And How To Use It – Everything You Need To Know