Jersey 2 JBoss Tutorial

Jersey 2 JBoss Tutorial explains step by step details of Creating / Developing Java rest Web services using Jersey, JBoss and Eclipse

JAX-RS is Java API for RESTful Webservices which is very rely upon Representational State Transfer model, you can view JAX-RS specification 

JAX-RS uses annotations for simplifying the development efforts.

Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs

Reference -> https://jersey.java.net

You can see the below example, which is demonstrating How to create a Restful service using Jersey 2 with JBoss

Note

If you are getting this error "java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;", when using Jersey 2 with JBoss. Please modify the web.xml and jboss-deployment-structure.xml as per the tutorial.

Required Libraries

You need to download

  1. JDK 7
  2. Eclipse 4.2
  3. Jersey 2.19
  4. JBoss EAP 6.2

Following jar must be in classpath

  1. hk2-api-2.4.0-b06.jar
  2. hk2-locator-2.4.0-b06.jar
  3. hk2-utils-2.4.0-b06.jar
  4. jackson-annotations-2.4.6.jar
  5. jackson-core-2.4.6.jar
  6. jackson-databind-2.4.6.jar
  7. jackson-jaxrs-base-2.3.2.jar
  8. jackson-jaxrs-json-provider-2.3.2.jar
  9. jackson-module-jaxb-annotations-2.3.2.jar
  10. javax.annotation-api-1.2.jar
  11. javax.inject-2.4.0-b25.jar
  12. javax.ws.rs-api-2.0.1.jar
  13. jersey-client.jar
  14. jersey-common.jar
  15. jersey-container-servlet-core.jar
  16. jersey-container-servlet.jar
  17. jersey-guava-2.19.jar
  18. jersey-media-json-jackson-2.14.jar
  19. jersey-server.jar
  20. validation-api-1.1.0.Final.jar

Jersey 2 JBoss Rest Tutorial

I am creating a sample restful service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "Jersey2JBossExample" according to following screenshot

Create Jersey JBoss Project Jersey JBoss Restful Tutorial

Create a Student Object

package com.entity;

public class Student {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Create a Controller

Here StudentController will defines which methods of restful service, to be invoked by the client

Here we are using one example showing with GET method & another with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

package com.controller;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.entity.Student;

@Path("Services")
public class StudentController {

@POST
  @Path
("/changeName")
@Produces(MediaType.APPLICATION_JSON)
public Student changeName(Student student) {
student.setName("HELLO " + student.getName());
return student;
}

@GET
  @Path
("/getName")
@Produces(MediaType.APPLICATION_JSON)
public Student getName() {
Student student = new Student();
    student.setName
("Rockey");
return student;
}
}

@Consumes annotation specifies, the request is coming from the client

you can specify the Mime type as @Consumes("application/xml"), if the request is in xml format

@Produces annotation specifies, the response is going to the client

you can specify the Mime type as @Produces ("application/xml"), if the response need to be in xml format

Create ApplicationConfig

Here we register the StudentController class, so that jersey will invoke properly

package com.config;

import org.glassfish.jersey.server.ResourceConfig;

import com.controller.StudentController;


public class ApplicationConfig extends ResourceConfig {

public ApplicationConfig() {
register(StudentController.class);
}
}

web.xml

JBoss already packed with it's own rest framework known as RESTEasy. In order to avoid the conflicts with Jersey, you need to explicitly turn off JBOSS RESTEasy scanning.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Jboss: Jersey</display-name>
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
  </context-param>
  <servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <!-- specify the application configruation -->
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

jboss-deployment-structure.xml

Even though we turn off JBOSS RESTEasy scanning, we need to create JBoss deployment descriptor because JBoss 6.2 includes dependencies of modules

<?xml version='1.0' encoding='UTF-8'?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
	<deployment>
		<exclude-subsystems>
			<subsystem name="resteasy" />
		</exclude-subsystems>
		<exclusions>
			<module name="javaee.api" />
			<module name="javax.ws.rs.api" />
			<module name="org.jboss.resteasy.resteasy-jaxrs" />
		</exclusions>
	</deployment>
</jboss-deployment-structure>

Publishing Jersey 2 JBoss Restful Service

Run Jersey Restful On JBoss

Jersey 2 Output

Jersey 2 response

Note

you can also see the examples of using GET and POST method Restful Client











Your email address will not be published. Required fields are marked *