Browsed by
Category: Jetty

CXF Jetty Example

CXF Jetty Example

This is a simple example of how to combine CXF and Jetty to get a simple web services up and running.

Pom for this example:

[code language=”xml”]
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>datadidit.helpful.hints</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>jetty-cxf</artifactId>
<name>jetty-cxf</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>
[/code]

 

Here is a simple REST endpoint:

[code language=”Java”]
package datadidit.helpful.hints.rest.cxf;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("hello")
public class HelloCXF {
@GET
@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
public Response sayHello(){
return Response.ok("Hello").build();
}

@GET
@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
@Path("{name}")
public Response sayHelloToSomeone(@PathParam("name") @DefaultValue("world") String name){
return Response.ok("Hello "+name).build();
}
}
[/code]

Configuration file(web.xml) necessary for configuring Jetty and CXF:

[code language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CXF Jetty Example</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<!– Name of the resource –>
<init-param>
<param-name>jaxrs.serviceClasses</param-name>
<param-value>
datadidit.helpful.hints.rest.cxf.HelloCXF
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
[/code]

With this set up I’m able to run the following steps to see my webservice from the browser:

  1. From root of project run ‘mvn jetty:run’
  2. Navigate to http://localhost:8080/hello and the word ‘Hello’ will  be shown to you from the browser
  3. Next navigate to http://localhost:8080/hello/world and the phrase ‘Hello world’ will be shown to you from the browser.
Hello Jersey and Jetty

Hello Jersey and Jetty

Seven Easy steps to get Jetty and Jersey working together

When you need a servlet and don’t want to have to run your own web server like JBoss or Tomcat, Jetty is very handy. I like to use JAX-RS annotations and am very familiar with Jersey so I wanted to write a step by step tutorial for how to combine Jetty and Jersey. Instructions assume you know how to use Maven .

1. Use mvn archetype:generate to create your java project. Archetypes make project set up so much easier because the archetype will generate a base for you.

2. Add the following dependencies to your pom:

[code language=”xml”]<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
</dependency>

[/code]

Note: jetty.version: 9.2.10.v20150310, jersey.version: 1.19

This should work with any non glassfish version of the jersey. I was having trouble getting the glassfish version to scan packages.

3. In order to be able to run mvn jetty:run from the command line we need to also add the jetty build plugin to our pom.xml. Add this plugin to your section of your pom.xml.

[code language=”xml”]<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>

[/code]

4. Make a simple Servlet class in the package of your choosing. Here’s an example of the ‘Hello’ Servlet:

[code language=”Java”]@Path("/hello")
public class App
{
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response sayHello(@DefaultValue("world") @QueryParam("name") String name){
return Response.ok("Hello "+name).build();
}
}

[/code]

The @Path annotation dictates the path to navigate from the ‘root’ of the web server to get to your servlet. The @GET annotation defines the method as a GET request. The @Produces annotation dictates the type of data that is returned when making this request.

5. Add a web.xml file in your src/main/webapp/WEB-INF folder:

[code language=”xml”]<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>MyApplication</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>mkw.jetty</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

[/code]

The ‘com.sun.jersey.spi.container.servlet.ServletContainer’ defines the servlet container to use. The important param in here is the ‘com.sun.jersey.config.property.packages’ Jersey will scan all classes found under the param value defined here. So it’s important for this param-value to match the package your Servlet code is contained in.

6. Run the command mvn jetty:run from the terminal(if on windows you can do this from the cmd prompt or Cygwin).

The end log from your prompt should look similar to this(minus the package names I used of course):

[code]

INFO: Scanning for root resource and provider classes in the packages:

mkw.jetty

May 17, 2015 6:04:40 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses

INFO: Root resource classes found:

class mkw.jetty.App

May 17, 2015 6:04:40 AM com.sun.jersey.api.core.ScanningResourceConfig init

INFO: No provider classes found.

May 17, 2015 6:04:41 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate

INFO: Initiating Jersey application, version ‘Jersey: 1.19 02/11/2015 03:25 AM’

2015-05-17 06:04:41.854:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@29d334c{/,file:/C:/Users/Marcus/workspace/jetty-tutorial/src/main/webapp/,AVAILABLE}{file:/C:/Users/Marcus/workspace/jetty-tutorial/src/main/webapp/}

2015-05-17 06:04:41.855:WARN:oejsh.RequestLogHandler:main: !RequestLog

2015-05-17 06:04:41.984:INFO:oejs.ServerConnector:main: Started ServerConnector@4aedaf61{HTTP/1.1}{0.0.0.0:8080}

2015-05-17 06:04:41.985:INFO:oejs.Server:main: Started @6755ms

[INFO] Started Jetty Server

[/code]

7. Now navigate to http://localhost:8080/hello?name=Test and your browser should be telling you hello.