Kleines Beispiel zu REST-Webservices mit JAX-RS und Jersey

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal ein kleines Beispiel zu Webservices mit JAX-RS und Jersey:

Unsere Resource:
Java:
package de.tutorials.training.jaxrs.examples.ex1.res;

import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/helloworld")
public class HelloWorldResource {
	@GET
	@Produces("text/plain")
	@Path("/sayHello/{someName}")
	public String sayHelloTo(@PathParam("someName") String someName){
		String msg = "Hello " + someName;
		Logger.getLogger(getClass().getName()).info("Server: " + msg);
		return msg;
	}
}

Unser (standalone) "Server":
Java:
package de.tutorials.training.jaxrs.examples.ex1;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;

public class Ex1Server {
	public static void main(String[] args) throws Exception {
		final String baseUri = "http://localhost:4711/";
		final Map<String, String> params = new HashMap<String, String>();

		params.put("com.sun.jersey.config.property.packages",
				"de.tutorials.training.jaxrs.examples.ex1.res");

		System.out.println("Starting web server...");

		HttpServer server = GrizzlyWebContainerFactory.create(baseUri,params);
		Logger.getLogger(Ex1Server.class.getName())
				.info(String
						.format("Jersey app started with WADL available at %sapplication.wadl\n"
								+ "Try out %shelloworld\nHit enter to stop it...",
								baseUri, baseUri));

		System.in.read();
		server.stop();
		System.exit(0);
	}
}

Unser Client:
Java:
package de.tutorials.training.jaxrs.examples.ex1;

import java.net.URL;
import java.util.Scanner;

public class Ex1Client {
	public static void main(String[] args) throws Exception{
		Scanner scanner = new Scanner(new URL("http://localhost:4711/helloworld/sayHello/tutorials.de").openStream());
		while(scanner.hasNextLine()){
			System.out.println(scanner.nextLine());
		}
		scanner.close();
	}
}

Hier noch das maven Pom.xml:
XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>de.tutorias</groupId>
	<artifactId>de.tutorials.training.jaxrs.examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
	<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-grizzly2</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>com.sun.grizzly</groupId>
			<artifactId>grizzly-servlet-webserver</artifactId>
			<version>1.9.18-i</version>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>maven2-repository.java.net</id>
			<name>Java.net Repository for Maven</name>
			<url>http://download.java.net/maven/2/</url>
			<layout>default</layout>
		</repository>
		<repository>
			<id>maven-repository.java.net</id>
			<name>Java.net Maven 1 Repository (legacy)</name>
			<url>http://download.java.net/maven/1</url>
			<layout>legacy</layout>
		</repository>
	</repositories>
</project>

Ausgabe Server:
Code:
Starting web server...
27.06.2011 19:16:48 org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:4711]
27.06.2011 19:16:48 org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
27.06.2011 19:16:48 de.tutorials.training.jaxrs.examples.ex1.Ex1Server main
INFO: Jersey app started with WADL available at http://localhost:4711/application.wadl
Try out http://localhost:4711/helloworld
Hit enter to stop it...
27.06.2011 19:16:52 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  de.tutorials.training.jaxrs.examples.ex1.res
27.06.2011 19:16:52 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class de.tutorials.training.jaxrs.examples.ex1.res.HelloWorldResource
27.06.2011 19:16:52 com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
27.06.2011 19:16:52 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
27.06.2011 19:16:53 de.tutorials.training.jaxrs.examples.ex1.res.HelloWorldResource sayHelloTo
INFO: Server: Hello tutorials.de

Ausgabe: Client:
Code:
Hello tutorials.de

Unter http://localhost:4711/application.wadl bekommen wir auch die WADL Beschreibung (Was WSDL bei normalen Webservices ist WADL für REST Web Services) unseres REST Webservices zu sehen.
Siehe auch:
http://searchsoa.techtarget.com/tip/WADL-The-REST-answer-to-WSDL
http://wadl.java.net/
Ausgabe:
XML:
<application>
	<doc jersey:generatedBy="Jersey: 1.8 06/24/2011 12:17 PM" />
	<resources base="http://localhost:4711/">
		<resource path="/helloworld">
			<resource path="/sayHello/{someName}">
				<param type="xs:string" style="template" name="someName" />
				<method name="GET" id="sayHelloTo">
					<response>
						<representation mediaType="text/plain" />
					</response>
				</method>
			</resource>
		</resource>
	</resources>
</application>

Gruß Tom
 
Zuletzt bearbeitet von einem Moderator:
Zurück