By kswaughs | Wednesday, November 25, 2015

CXF Rest Client to call REST service - GET method

I have a sample REST service with GET method running in my local system. This method is used to fetch user details..

The URL is ''

Response is

   "status": "Success",
   "id": "123",
   "name": "kswaughs"

Create a model object to parse JSON response into java object.

Response Object - User
package com.example.cxfrs.beans;

public class User {

 private String status;

 private String id;

 private String name;

 public String getStatus() {
  return status;

 public void setStatus(String status) {
  this.status = status;

 public String getId() {
  return id;

 public void setId(String id) { = id;

 public String getName() {
  return name;

 public void setName(String name) { = name;

 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("User [status=");
  builder.append(", id=");
  builder.append(", name=");
  return builder.toString();

A simple stand-alone client code using WebClient and JacksonJsonProvider.

Sample Client code to call get Method
package com.example.cxfrs;

import java.util.ArrayList;
import java.util.List;

import com.example.cxfrs.beans.User;
import org.apache.cxf.jaxrs.client.WebClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

public class CXFRestClient {

 public static void main(String[] args) {

  List<Object> providers = new ArrayList<Object>();
  providers.add(new JacksonJsonProvider());

  WebClient client = WebClient
    .create("", providers);
  User user = client.accept("application/json").type("application/json")


Output is

User [status=Success, id=123, name=kswaughs]

Use Below maven dependency in your pom.xml to use CXF Client

<!-- To use WebClient API -->
<!-- To parse JSON String to Java objects --> 

If you dont provide JacksonJsonProvider to WebClient, You will get below error while parsing the JSON response.

org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem
SEVERE: No message body reader has been found for class com.example.cxfrs.beans.User, ContentType: application/json
Exception in thread "main" No message body reader has been found for class org.apache.camel.example.cxfrs.beans.User, ContentType: application/json
 at org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(
 at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(
 at org.apache.cxf.jaxrs.client.AbstractClient.readBody(
 at org.apache.cxf.jaxrs.client.WebClient.handleResponse(
 at org.apache.cxf.jaxrs.client.WebClient.doResponse(
 at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(
 at org.apache.cxf.jaxrs.client.WebClient.doInvoke(
 at org.apache.cxf.jaxrs.client.WebClient.doInvoke(
 at org.apache.cxf.jaxrs.client.WebClient.invoke(
 at org.apache.cxf.jaxrs.client.WebClient.get(
 at com.example.cxfrs.CXFRestClient.main(

Recommend this on