HOWTO Build a REST-Application with Jersey and Jetty

Resource first:


package com.tutego.traida.server;

import javax.ws.rs.*;

@Path( "/" )
public class GreetingResource
{
@GET @ProduceMime("text/plain")

public String get()
{
return "Yea!";
}
}

Bring it to Jetty


package com.tutego.traida.server;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;

public class Application 
{
public static void main( String[] args ) throws Exception
{
ServletHolder sh = new ServletHolder( ServletContainer.class );

sh.setInitParameter( "com.sun.jersey.config.property.resourceConfigClass",
"com.sun.jersey.api.core.PackagesResourceConfig" );
sh.setInitParameter( "com.sun.jersey.config.property.packages", "com.tutego.traida.server" );

Server server = new Server( 9999 );
Context context = new Context( server, "/", Context.SESSIONS );

context.addServlet( sh, "/*" );
server.start();

}
}

To use REST-parameters


@Path( "/edit-customer/{customerid}" )
public class EditCustomerResource
{
@GET @ProduceMime("text/plain")
public String editUser( @PathParam("customerid") String customerId )
{
....
}
}

0 Responses to “HOWTO Build a REST-Application with Jersey and Jetty”

Kommentar veröffentlichen

Suche

Willkommen!

In diesem Blog schreibe ich über aktuelle Entwicklungen in Java, Softwareentwicklung im Allgemeinen, Open-Source, JavaScript, neue Eclipse-Plugins und Neuerungen im Buch ›Java ist auch eine Insel‹.

Mein Foto
Name: Christian Ullenboom
Standort: Hannover, Niedersachsen, Germany

Ich bin Christian Ullenboom und Autor des Buches ›Java ist auch eine Insel‹. Seit 1997 schule ich als Java-Tutor Themen wie Java SE, EJB 3, Hibernate, JSP/Servlets. Sun ernannte mich 2005 zum ›Java-Champion‹.

Feed hinzufügen

  • AddThis Feed Button


XML