Monthly Archives: February 2016

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
org.codehaus.jackson.map.annotate.JsonView

Below is the maven artifact for downloading this:

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

 

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 :

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-servlet.xml</param-value>
</context-param>


<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>

</servlet>

 

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

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

 

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{

@POST
@Path("/diagnostic_sessions/getSearchResult")
@Produces(MediaType.APPLICATION_JSON)
@JsonView(ResultView.class)
public GenericDataView getSearchResultForJsonView(@Description("Diagnostic Session ID") GenericDataView genericDataView)throws Exception;

}

 

Controller implementation will be:

@Component("searchServiceImpl")
public class SearchServiceImpl implements SearchService {

@Override
public GenericDataView getSearchResultForJsonView(GenericDataView genericDataView) throws Exception {
GenericDataView dataView= new GenericDataView();
dataView.setUsername(genericDataView.getUsername());
dataView.setEmail(genericDataView.getEmail());
dataView.setEmpCode("CTYOP890");
dataView.setAddress("Chicago, America");
dataView.setPhone("681432413");
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 {

@JsonView(ParamView.class)
String username;

@JsonView(ParamView.class)
String email;

@JsonView(ResultView.class)
String empCode;

@JsonView(ResultView.class)
String phone;

@JsonView(ResultView.class)
String address;
}

 

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

{

"username": "ABCD",
"email": "abcd@xyz.com"

}

 

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": "abcd@xyz.com",
 "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-  parry.ism@gmail.com