Searching...
Friday, 8 January 2016

JAX-RS RESTFul Service with Jetty server

22:08
In this example we will see simple JAX-RS restful web service using Jetty service and Jersey implementaion of JAX-RS API

1. Create a maven project using the quick start archetype.

2. Add below dependencies in your pom.xml file
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
3. Create a simple service class which returns Hello world
package com.vinod.vinod_rest_examples;


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

@Path("/MyFirstRest")
public class MyFirstRest {

@GET
@Path("helloworld")
@Produces(MediaType.APPLICATION_JSON)
public String test() {
return "Hello world";
}
}
4. Create a java class to start jetty server
package com.vinod.vinod_rest_examples;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 * Hello world!
 *
 */
public class App {
public static void main(String[] args) {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");

Server jettyServer = new Server(8080);
jettyServer.setHandler(context);

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);

// Setting pacakge name over here to load services
jerseyServlet.setInitParameter("jersey.config.server.provider.packages",
"com.vinod.vinod_rest_examples");
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
jettyServer.destroy();
}
}
}
5. Hit the url
 
6. Output
Hello world

 
8. What is next?.. let us see how to add Request Filter over here.
Filters can modify inbound and outbound requests and responses such as modification of headers, entity and other request/response parameters
package com.vinod.vinod_rest_examples;

import java.io.IOException;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;

@Provider
@Priority(value = 1)
public class MyRequestFilter implements ContainerRequestFilter {

public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("Executing request filter" + requestContext.getUriInfo().getPath());
}

}
filter output

Executing request filter/MyFirstRest/helloworld

 

0 comments: