Jersey creates standalone server 1

Source: Internet
Author: User
Tags glassfish

Sometimes, we need to create a web service, but do not need to deploy it in a container. Instead, we need to run it like an application, listen to the port, parse the HTTP request, and send an HTTP response.

This can occupy less resources. For example, I have encountered a rented server that cannot be started by the Glassfish server due to the number of core threads. Many system service programs are implemented like this, such as Gerrit2.

Jersey also provides this method by integrating grizzly2. now let's see how to use it.

First, use maven archetype to find the appropriate template from the java site:

[Plain]
Mvn archetype: generate-DarchetypeCatalog = http://download.java.net/maven/2
You will soon be prompted to select the template:
[Plain]
[INFO] No archetype defined. Using maven-archetype-quickstart (org. apache. maven. archetypes: maven-archetype-quickstart: 1.0)
Choose archetype:
1: http://download.java.net/maven/2-> com. sun. jersey. archetypes: jersey-quickstart-grizzly (Archetype for creating a RESTful web application with Jersey and Grizzly)
2: http://download.java.net/maven/2-> com. sun. jersey. archetypes: jersey-quickstart-grizzly2 (Archetype for creating a RESTful web application with Jersey and Grizzly 2.x)
3: http://download.java.net/maven/2-> com. sun. jersey. archetypes: jersey-quickstart-webapp (Archetype for creating a Jersey based RESTful web application with WAR packaging)
4: http://download.java.net/maven/2-> com. sun. jersey. archetypes: jersey-quickstart-ejb (Archetype for creating a Jersey based RESTful EJB application with WAR packaging)
5. http://download.java.net/maven/2-> com. sun. faces: simple-jsf (Archetype for creating a simple JSF project)
6: http://download.java.net/maven/2-> com. sun. faces. regression: I _jsf_XXXX-archetype (Archetype for mojarra JSF regression tests)

I chose 2.
Then fill in information such as groupId and artifactID.

[Plain] view plaincopyprint?
Define value for property 'groupid': com. esri
Define value for property 'artifactid': carrier
Define value for property 'version': 1.0-SNAPSHOT ::
Define value for property 'package': com. esri ::
Confirm properties configuration:
GroupId: com. esri
ArtifactId: carrier
Version: 1.0-SNAPSHOT
Package: com. esri
Y: y

After a period of download, everything is ready. A project is created.
Start it after compilation:

[Plain]
Mvn exec: java
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the valid tive model for com. esri: carrier: jar: 1.0-SNAPSHOT
[WARNING] 'build. plugins. plugin. version' for org. apache. maven. plugins: maven-compiler-plugin is missing. @ line 58, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building carrier 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin: 1.1: java (default-cli) @ carrier >>>
[INFO]
[INFO] <exec-maven-plugin: 1.1: java (default-cli) @ carrier <
[INFO]
[INFO] --- exec-maven-plugin: 1.1: java (default-cli) @ carrier ---
Starting grizzly2...
Nov 6, 2012 1:10:27 org. glassfish. grizzly. http. server. NetworkListener start
INFO: Started listener bound to [localhost: 9998]
Nov 6, 2012 1:10:27 org. glassfish. grizzly. http. server. HttpServer start
INFO: [HttpServer] Started.
Jersey app started with WADL available at http: // localhost: 9998/application. wadl
Hit enter to stop it...

The local port 9998 is listened on.
Open the following URL in a browser:

[Plain]
Http: // localhost: 9998/application. wadl

See the following page:
[Plain]
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Application xmlns = "http://research.sun.com/wadl/2006/10">
<Doc xmlns: jersey = "http://jersey.java.net/" jersey: generatedBy = "Jersey: 1.8 06/24/2011 PM"/>
<Resources base = "http: // localhost: 9998/">
<Resource path = "/myresource">
<Method name = "GET" id = "getIt">
<Response>
<Representation mediaType = "text/plain"/>
</Response>
</Method>
</Resource>
</Resources>
</Application>

Describes a REST web service method provided by this application. The URL path is/myresource.
Access this address in a browser:
Http: /localhost: 9998/myresource

The following result is displayed.

Got it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.