Liferay Jersey Integration
Liferay Jersey Integration tutorial will drive you on how to create web services using jersey framework. By default, Liferay provides axis engine framework for SOAP and JSON web services with Service Builder, that generates local service and remote service layers, but still it has some limitations. Liferay with jersey offers very simple and easy implementation of web services.
Click here to access REST Tutorials with Jersey and Spring will give complete list of tutorials for beginners.
Liferay Remote web services limitations:
- Liferay JSON web services allows only param request only. i.e you need to send whole data as request parameters and does not support JSON Object payload type.
- Lifeary JSON web services allows only Service Builder entity to expose outside, what if you want expose custom entity as JSON?
Liferay Jersey web services limitations:
- Need of Custom security implementation. This can be overcome with simple filter implementation
- Liferay implicit functions and objects such as companyId and permission checker and all.
Let’s jump into Liferay Jersey rest tutorial and Download source code from here.
- Create portlet plugin project to hold all Web services
- Download jersey-bundle-1.19.jar and jackson-all-1.9.0.jar. copy both of them in to lib folder
- Configure Jersey servlet in web.xml
-
<servlet> <servlet-name>jersey-rest-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>org.javasavvy.rest.controller</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-rest-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
-
- Create package with below structure
- In Jersey init parameter, configure “com.sun.jersey.config.property.packages” to controller path “org.javasavvy.rest.controller”
- Set com.sun.jersey.api.json.POJOMappingFeature to true in init parameters to enable auto marshalling and unmarshalling of POJO’s to JSON or XML.
- Create a controller class that annotated with @Path and here you can see ProductController class
-
@Path(value="/product") public class ProductController { @GET @Path("/getProductDetails") @Produces(MediaType.APPLICATION_JSON) public Response getProductDetails(){ ProductModal pr = new ProductModal(); pr.setDescription("Liferay"); pr.setName("Liferay"); pr.setProductId(1L); System.out.println("ehits "); return Response.status(200).entity(pr).build(); } @POST @Path("/addProduct") @Consumes(MediaType.APPLICATION_JSON) public Response addProduct(ProductModal pr){ StatusModal st = new StatusModal(); st.setMessage("Product Created Successfully"); st.setStatus(200); return Response.status(200).entity(st).build(); } }
-
- Create POJO modal that need to annotate with @XmlRootElement to auto marshalling and unmarshalling of Java POJO to JSON
- If application requires to produce both XML and JSON then update @Produces with XML media type as shown below
-
@GET @Path("/getProductDetails") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
-
- Create Modal Object that need to annotate with @XmlRootElement
- Finally deploy the portlet and access below URL
- Request URI : http://localhost:8080/liferay-jersy-portlet/rest/product/getProductDetails
- XML Response:
<productModal><description>Liferay</description><name>Liferay</name><productId>1</productId></productModal>
- JSON Response:
{"productId":1,"name":"Liferay","description":"Liferay"}
Download the liferay jersey rest source code from here.
Hope this helps.
3 thoughts on “liferay jersey integration”
Comments are closed.
Nice and Well explained Example!!!
Thanks a lot.
This example is not getting deployed in Weblogic 12.
getting below error while deploying via admin console:
Message icon – Error Unable to access the selected application.
Message icon – Error javax.servlet.Filter
Message icon – Error javax.servlet.Filter
Message icon – Error javax.servlet.Filter
Is it possible to push this RESTFull service to ROOT context of Liferay using ext ???