Tuesday November 25, 2008
TOTD #56: Simple RESTful Web service using Jersey and Embeddable GlassFish - Text and JSON output
![]() |
Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services in the GlassFish community. It also provides an API that allows developers to extend Jersey to suite their requirements. |
| ~/samples/jersey >mvn archetype:generate
-DarchetypeCatalog=http://download.java.net/maven/2 [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:generate] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Preparing archetype:generate [INFO] No goals needed for project - skipping [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:generate] [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: 1: remote -> jersey-quickstart-grizzly (Archetype for creating a RESTful web application with Jersey and Grizzly) 2: remote -> jersey-quickstart-webapp (Archetype for creating a Jersey based RESTful web application WAR packaging) Choose a number: (1/2): 2 [INFO] snapshot com.sun.jersey.archetypes:jersey-quickstart-webapp:1.0.1-SNAPSHOT: checking for updates from jersey-quickstart-webapp-repo Define value for groupId: : org.glassfish.samples Define value for artifactId: : helloworld-webapp Define value for version: 1.0-SNAPSHOT: : Define value for package: : org.glassfish.samples Confirm properties configuration: groupId: org.glassfish.samples artifactId: helloworld-webapp version: 1.0-SNAPSHOT package: org.glassfish.samples Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: jersey-quickstart-webapp:1.0.1-SNAPSHOT [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: org.glassfish.samples [INFO] Parameter: packageName, Value: org.glassfish.samples [INFO] Parameter: package, Value: org.glassfish.samples [INFO] Parameter: artifactId, Value: helloworld-webapp [INFO] Parameter: basedir, Value: /Users/arungupta/samples/jersey [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: /Users/arungupta/samples/jersey/helloworld-webapp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 21 seconds [INFO] Finished at: Mon Nov 24 14:09:27 PST 2008 [INFO] Final Memory: 12M/30M [INFO] ------------------------------------------------------------------------ |
|
<plugin> <groupId>org.glassfish</groupId> <artifactId>maven-glassfish-plugin</artifactId> </plugin> |
|
<pluginRepositories> <pluginRepository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </pluginRepository> <pluginRepository> <id>maven-repository.dev.java.net</id> <name>Java.net Maven 1 Repository (legacy)</name> <url>http://download.java.net/maven/1</url> <layout>legacy</layout> </pluginRepository> </pluginRepositories> |
|
<dependency> <groupId>org.glassfish.distributions</groupId> <artifactId>web-all</artifactId> <version>10.0-build-20080430</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.embedded</groupId> <artifactId>gf-embedded-api</artifactId> <version>1.0-alpha-4</version> <scope>test</scope> </dependency> |
|
<dependency> <groupId>org.glassfish.distributions</groupId> <artifactId>web-all</artifactId> <version>10.0-SNAPSHOT</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.embedded</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>3.0-Prelude-SNAPSHOT</version> </dependency> |
|
<properties> <jersey-version>1.0</jersey-version> </properties> |
| package org.glassfish.samples; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; // The Java class will be hosted at the URI path "/helloworld" @Path("/myresource") public class MyResource { // The Java method will process HTTP GET requests @GET // The Java method will produce content identified by the MIME Media // type "text/plain" @Produces("text/plain") public String getIt() { return "Hi there!"; } } |
| ~/samples/jersey/helloworld-webapp >mvn glassfish:run [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'glassfish'. [INFO] ------------------------------------------------------------------------ [INFO] Building helloworld-webapp Jersey Webapp [INFO] task-segment: [glassfish:run] [INFO] ------------------------------------------------------------------------ [INFO] Preparing glassfish:run [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Compiling 1 source file to /Users/arungupta/samples/jersey/helloworld-webapp/target/classes [INFO] [glassfish:run] Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: HK2 initialized in 229 ms Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.naming.impl.ServicesHookup@2470b02c Init done in 237 ms Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.v3.server.Globals@13b3d787 Init done in 239 ms Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.v3.server.SystemTasks@61bedd7d Init done in 244 ms Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@2b9f7952 Init done in 245 ms Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@5249d560 Init done in 248 ms JMXMP connector server URL = service:jmx:jmxmp://localhost:8888 Nov 24, 2008 2:36:05 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start INFO: Listening on port 8080 Nov 24, 2008 2:36:06 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@1baa56a2 startup done in 551 ms Nov 24, 2008 2:36:06 PM com.sun.enterprise.v3.services.impl.ApplicationLoaderService postConstruct INFO: loader service postConstruct started at 1227566166208 Nov 24, 2008 2:36:06 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: Application Loader startup done in 740 ms Nov 24, 2008 2:36:06 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: Glassfish v3 started in 740 ms Nov 24, 2008 2:36:07 PM com.sun.enterprise.web.WebModuleContextConfig authenticatorConfig SEVERE: webModuleContextConfig.missingRealm Nov 24, 2008 2:36:07 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Scanning for root resource and provider classes in the packages: org.glassfish.samples Nov 24, 2008 2:36:07 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Root resource classes found: class org.glassfish.samples.MyResource Nov 24, 2008 2:36:07 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Provider classes found: Hit ENTER for redeploy |


| package org.glassfish.samples; import javax.xml.bind.annotation.XmlRootElement; /** * @author arungupta */ @XmlRootElement public class Greeting { public String greeting; public Greeting() { } public Greeting(String greeting) { this.greeting = greeting; } } |
| //
@Produces("text/plain") @Produces("application/json") public Greeting getIt() { return new Greeting("Hi there!"); } |

| mvn clean package |
Posted by Arun Gupta in webservices | Comments[16]
|
|
|
|
|
Today's Page Hits: 2466
Total # blog entries: 994
| « November 2009 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
1 | 2 | 4 | 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 | |||||
| Today | ||||||
Nice post! I have updated the published Jersey archetype, so that steps 2.1 and 2.2 should not be needed any more.
Posted by Jakub on November 25, 2008 at 10:02 AM PST #
One more comment on generating the sample. You can also use non-interactive version:
mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 -DinteractiveMode=false -DarchetypeArtifactId=helloworld-webapp -DarchetypeGroupId=com.sun.jersey.archetypes -DgroupId=org.glassfish.samples -DartifactId=test -Dpackage=org.glassfish.samples
Posted by Jakub on November 25, 2008 at 10:23 AM PST #
Thanks a lot Jakub, this will further simplify the development process and this blog entry. However the mvn command is throwing NPE, any idea ?
Posted by Arun Gupta on November 25, 2008 at 10:52 AM PST #
Arun: i did not test the command previously, but this time the following should work:
mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 -DinteractiveMode=false -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=com.sun.jersey.archetypes -DgroupId=org.glassfish.samples -DartifactId=helloworld-webapp -Dpackage=org.glassfish.samples
Posted by Jakub on November 25, 2008 at 02:03 PM PST #
Hmm, getting a NPE again. Here is what I see:
~/samples/jersey >mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 -DinteractiveMode=false -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=com.sun.jersey.archetypes -DgroupId=org.glassfish.samples -DartifactId=helloworld-webapp -Dpackage=org.glassfish.samples
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] : java.lang.NullPointerException
null
Posted by Arun Gupta on November 25, 2008 at 05:01 PM PST #
Nice blog although I would have chosen:
...
Choose a number: (1/2): 1
...
Much faster ;-)
Posted by Jeanfrancois Arcand on November 25, 2008 at 08:34 PM PST #
Posted by Arun Gupta's Blog on November 26, 2008 at 06:03 AM PST #
Jeanfrancois, that is already explained in the tech tip at: http://blogs.sun.com/enterprisetechtips/entry/configuring_json_for_restful_web :)
These blogs are prelude for a bigger blog coming up next week, stay tuned and you'll understand the context!
Posted by Arun Gupta on November 26, 2008 at 06:06 AM PST #
NPE again: i do not know, my maven says:
%mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 -DinteractiveMode=false -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=com.sun.jersey.archetypes -DgroupId=org.glassfish.samples -DartifactId=helloworld-webapp -Dpackage=org.glassfish.samples | tee mvn.out
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Batch mode
[INFO] Archetype [com.sun.jersey.archetypes:jersey-quickstart-webapp:1.0.1-SNAPSHOT -> http://download.java.net/maven/2] found in catalog http://download.java.net/maven/2
[INFO] snapshot com.sun.jersey.archetypes:jersey-quickstart-webapp:1.0.1-SNAPSHOT: checking for updates from jersey-quickstart-webapp-repo
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: jersey-quickstart-webapp:1.0.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.glassfish.samples
[INFO] Parameter: packageName, Value: org.glassfish.samples
[INFO] Parameter: basedir, Value: /export/home/japod/workspace
[INFO] Parameter: package, Value: org.glassfish.samples
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: helloworld-webapp
[WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 46,column 22] : ${jersey-version} is not a valid reference.
[WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 51,column 22] : ${jersey-version} is not a valid reference.
[WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 62,column 22] : ${jersey-version} is not a valid reference.
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /export/home/japod/workspace/helloworld-webapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Nov 26 16:25:25 CET 2008
[INFO] Final Memory: 11M/167M
[INFO] ------------------------------------------------------------------------
Posted by Jakub on November 26, 2008 at 07:32 AM PST #
Posted by Arun Gupta's Blog on December 01, 2008 at 06:31 AM PST #
Jakub,
It seems like this might be a platform specific problem, will debug later.
Posted by Arun Gupta on December 03, 2008 at 01:09 PM PST #
If the generated dependencies:
<dependency> <groupId>org.glassfish.distributions</groupId>
<artifactId>web-all</artifactId>
<version>10.0-build-20080430</version>
<scope>test</scope>
</dependency>
and
<dependency> <groupId>org.glassfish.embedded</groupId>
<artifactId>gf-embedded-api</artifactId>
<version>1.0-alpha-4</version>
<scope>test</scope>
</dependency>
are changed to:
<dependency>
<groupId>org.glassfish.distributions</groupId>
<artifactId>web-all</artifactId>
<version>10.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
and
<dependency>
<groupId>org.glassfish.embedded</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0-Prelude-SNAPSHOT</version>
</dependency>
then the latest version of the embedded APIs are used.
Posted by Arun Gupta on December 03, 2008 at 01:16 PM PST #
Posted by Arun Gupta's Blog on December 04, 2008 at 07:12 AM PST #
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Betty
http://www.my-foreclosures.info
Posted by Betty on December 16, 2008 at 07:00 PM PST #
ThankSss
Posted by dizi izle on February 21, 2009 at 09:17 AM PST #
thx u. perfect and beautiful expression.
Posted by sinema izle on March 07, 2009 at 03:32 PM PST #