Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server

Maven Dependencies

The following Maven dependencies need to be added to the pom:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-grizzly2</artifactId>
    <version>1.19.1</version>
</dependency>

Creating a root resource

Create the following Java class in your project:

package com.huey.hello.jersey.resources;

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("helloworld")
public class HelloWorldResource {

    // 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 sayHello() {
         // Return the textual content
        return "Hello World";
    }

}

The HelloWorldResource class is a very simple Web resource. The URI path of the resource is "/helloworld", it supports the HTTP GET method and produces cliched textual content of the MIME media type "text/plain".

Deploying the root resource

The root resource will be deployed using the Grizzly Web container.

 1 package com.huey.hello.jersey;
 2
 3 import java.io.IOException;
 4 import java.net.URI;
 5
 6 import javax.ws.rs.core.UriBuilder;
 7
 8 import org.glassfish.grizzly.http.server.HttpServer;
 9
10 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
11 import com.sun.jersey.api.core.PackagesResourceConfig;
12 import com.sun.jersey.api.core.ResourceConfig;
13
14 public class HelloJersey {
15
16     private static URI getBaseURI() {
17         return UriBuilder.fromUri("http://localhost/").port(9998).build();
18     }
19
20     public static final URI BASE_URI = getBaseURI();
21
22     protected static HttpServer startServer() throws IOException {
23         System.out.println("Starting grizzly...");
24         ResourceConfig rc = new PackagesResourceConfig("com.huey.hello.jersey.resources");
25         return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
26     }
27
28     public static void main(String[] args) throws IOException {
29         HttpServer httpServer = startServer();
30         System.out.println(String.format("Jersey app started with WADL available at "
31                         + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
32                         BASE_URI, BASE_URI));
33         System.in.read();
34         httpServer.stop();
35     }
36 }

The HelloJersey class deploys the HelloWorldResource using the Grizzly Web container.

Line 24 creates an initialization parameter that informs the Jersey runtime where to search for root resource classes to be deployed. In this case it assumes the root resource class in the package com.huey.hello.jersey.resources (or in a sub-package of).

Line 25 deploys the root resource to the base URI "http://localhost:9998/" and returns a Grizzly HttpServer. The complete URI of the Hello World root resource is "http://localhost:9998/helloworld".

Testing the root resource

Goto the URI http://localhost:9998/helloworld in your favourite browser.

Or, from the command line use curl:

时间: 2024-10-26 08:46:36

Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server的相关文章

Jersey(1.19.1) - Hello World

1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </properties> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <versi

Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses

Previous sections have shown how to return HTTP responses and it is possible to return HTTP errors using the same mechanism. However, sometimes when programming in Java it is more natural to use exceptions for HTTP errors. The following example shows

Jersey(1.19.1) - Life-cycle of Root Resource Classes

By default the life-cycle of root resource classes is per-request, namely that a new instance of a root resource class is created every time the request URI path matches the root resource. This makes for a very natural programming model where constru

Jersey(1.19.1) - Deploying a RESTful Web Service

JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes.

Jersey(1.19.1) - Client API, Proxy Configuration

为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args) { final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); Client client = new Client(new URLConnectionClientH

Jersey(1.19.1) - Security with Http(s)URLConnection

With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie management with Http(s)URLConnection is limited due to constraints in the API. There are currently no specific features or properties on the Client cla

Jersey(1.19.1) - Using filters

Filtering requests and responses can provide useful functionality that is hidden from the application layer of building and sending requests, and processing responses. Filters can read/modify the request URI, headers and entity or read/modify the res

Jersey(1.19.1) - Uniform Interface Constraint

The Jersey client API is a high-level Java based API for interoperating with RESTful Web services. It makes it very easy to interoperate with RESTful Web services and enables a developer to concisely and efficiently implement a reusable client-side s

Jersey(1.19.1) - XML Support

As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incoming request and create outgoing responses. Every user can create its own representation but... this is not recommended way how to do things. XML is prov