Using of JsonView with Apache CXF (JAX-RS API implementation).

By using JsonView, we need only one Json data view class for both input and output Json data.And in the output response , we can create dynamic response data
on the basic of type of operation in a method or the role of the user who is calling this method.
JsonView is a feature provided by Jackson library. Jar file which containing this feature is jackson-mapper-asl.jar. Class which should be used for JsonView is

Below is the maven artifact for downloading this:



We can use JsonView feature for JAX-RS based web service(REST Web Services).JsonView works with various JAX-RS  implementation like Apache CXF, Jeresy etc.In this article, we will use Apache CXF (version- 2.5.0). Below are the configuration for Apache CXF.

Add below configuration in web.xml :


<display-name>CXF Servlet</display-name>



And add below configuration in cxf-servlet.xml file:

<jaxrs:server id="searchservice" address="/api">
<ref bean="searchServiceImpl" />
<bean id="jacksonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"></bean>


Controller interface method declaration is annotated with @JsonView (ResultView.class).  In this API, GenericDataView class object will be used for both input and output.In input only two attribute(username and email) will be used.

Below is the controller interface in which API is defined.

@Api(value = "/searchservice", description = "searchservice REST API")
public interface SearchService{

public GenericDataView getSearchResultForJsonView(@Description("Diagnostic Session ID") GenericDataView genericDataView)throws Exception;



Controller implementation will be:

public class SearchServiceImpl implements SearchService {

public GenericDataView getSearchResultForJsonView(GenericDataView genericDataView) throws Exception {
GenericDataView dataView= new GenericDataView();
dataView.setAddress("Chicago, America");
return dataView;




Now lets see how @JsonView works.
Scenario-1: First lets define the type of views for the attributes in View Object.

public class Views {
static class ParamView { }
static class ResultView{ }



Our Data Model class for View object is:

public class GenericDataView {

String username;

String email;

String empCode;

String phone;

String address;


Now lets deploy the above REST API , and send the below json request:


"username": "ABCD",
"email": ""



Output Json response from the above REST API  will be:

 "empCode": "CTYOP890",
 "phone": "681432413",
 "address": "Chicago, America"


In output only empCode,phone,address attributes will be displayed in Json output which are annotated with ResultView(Same view with which above REST API is annotated).

Scenario-2:Lets use below views(ParamView extends ResultView).

public class Views {
static class ParamView { }
static class ResultView extends ParamView { }


We extends ResultView from ParamView here,So with the same request ,in output all the attributes(annotated with @ParamView and @ResultView will be displayed)

 "username": "ABCD",
 "email": "",
 "empCode": "CTYOP890",
 "phone": "681432413",
 "address": "Chicago, America"


So all the attributes username, email ,empCode,phone,address will be displayed in json output.

That is it about working of @JsonView with JAX-RS API implementation.Still if you face any issue, Please let me know at-

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s