How to enable Jersey REST api in a maven web project
Tech-Today

How to enable Jersey REST api in a maven web project


This tutorial assumes that you already know how to create a maven project in eclipse. Steps: 1.) Create a new maven web project. (this assumes that you have maven plugin installed in your eclipse). 2.) In your META-INF/web.xml file make sure that you have the ff code:

<servlet>
<servlet-name>Jersey REST Service</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>com.ipiel.xxx</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
3.) Note the value of com.sun.jersey.config.property.packages, this tells us that it will examine the classes inside com.ipiel.xxx for REST enabled web services.

@Path("/hello")
public class Hello {
@GET
@Path("/world")
public Response sayHello() {
String hello = "Hello World!";
return Response.status(200).entity(hello).build();
}
}
4.) Make sure that you have the ff definition in your pom.xml

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
5.) Compile and deploy the war/ear in a container (jboss, glassfish, etc). 6.) Now you can access the web app in your localhost: http://localhost:port/appname/api/hello/world And it should output "Hello World!". Note: In case you want to add parameter to your web service, this is how you will code it:

@Path("/reverse")
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces({ MediaType.APPLICATION_JSON })
public Response reverse(@FormParam("orderId") long orderId) {
}




- Secure Spring Boot Rest Project With Keycloak
1. OverviewIn this blog, we will cover the basics of securing a Spring project with Keycloak using keycloak-spring-boot-starter and keycloak-spring-security-adapter.2. LimitationKeycloak is already a well-documented topic that needs no further write up....

- How To Generate Wadl Via Maven Plugin
So you've written your web service and would now want to create the wadl for it.  Assuming you have your web project in eclipse with maven integrated this is what you have to do: 1.) Add the lines below to your pom, you may want to create it's...

- How To Use Primefaces With Jboss 7.1.3
This tutorial will teach us how to use primefaces with jboss 7.1.3. Requirements:  1.) primefaces 3.5  2.) jboss 7.1.3  3.) maven javaee6 generated project (ear, war, ejb) Steps: 1.) Add primefaces module in jboss.   a.) In JBOSS_HOME/modules...

- How To Call A Stateless Ejb From A Spring Rest Servlet Component
In my recent project I was ask to develop a rest servlet (using jersey) in a spring web project that would call an stateless EJB, where the business logic is.  This project is deployed on glassfish. After several hours I was able to figure out how...

- How To Auto Wire A Spring Bean From A Jsf Managed Bean In Liferay Portlet
There are 2 ways I know to auto-wire a spring bean into a jsf managed bean: 1.) Is through WebApplicationContext, invoke during jsf managed bean constructor: Below are the most vital files to perform this action: web.xml <?xml version="1.0"?>...



Tech-Today








.