zoukankan      html  css  js  c++  java
  • Jersey(1.19.1)

    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
    Try out %shelloworld
    Hit 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:

    [huey@huey-K42JE ~]$ curl -i http://localhost:9998/helloworld
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Date: Wed, 13 Apr 2016 14:14:40 GMT
    Transfer-Encoding: chunked
    
    Hello World
  • 相关阅读:
    Windows 下安装 swoole 具体步骤
    Mysql 连接提示 Client does not support authentication protocol requested by server 客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端
    mysql8 安装
    在 apache 配置 python-django
    QQWry.dat导入Mysql显IP程序
    PHP根据身份证号码判断星座性别生肖身份证号15位转18位
    layer---table 处理 方法设定使用 时间戳转换
    数字的处理 :小数点四舍五入
    javascript 检测手机设备 百度siteapp下的一款跳转的产品,使用起来很方便。你可以用这款JS跳转到手机版,也可以跳转到任何你想跳转的位置。
    基于Token的多平台身份认证价格设计
  • 原文地址:https://www.cnblogs.com/huey/p/5389198.html
Copyright © 2011-2022 走看看