ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
837
837
EMPFEHLEN
-
27.06.11 19:17 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
hier mal ein kleines Beispiel zu Webservices mit JAX-RS und Jersey:
Unsere Resource:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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":
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14
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:
Code xml:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<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 :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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 :1
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/...answer-to-WSDL
http://wadl.java.net/
Ausgabe:
Code xml:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<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ß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
Kleines Beispiel zur Verwendung der Derby DB in Java 6
Von Thomas Darimont im Forum JavaAntworten: 15Letzter Beitrag: 12.08.10, 11:58 -
Restful Webservices with Jersey and Java - Tutorial
Von vogella im Forum Java Technology NewsAntworten: 0Letzter Beitrag: 18.02.10, 04:33 -
Kleines Beispiel zum Thema Java 3D
Von Thomas Darimont im Forum JavaAntworten: 1Letzter Beitrag: 13.11.08, 19:35 -
Kleines Beispiel zur JExcel API
Von Thomas Darimont im Forum JavaAntworten: 3Letzter Beitrag: 11.09.07, 09:42 -
Kleines Beispiel zu RMI over IIOP
Von Thomas Darimont im Forum JavaAntworten: 0Letzter Beitrag: 24.05.05, 19:10






Zitieren
Login





