• 基于Java
    SE
    形式的REST服务
  • 创建项目

    我们首选使用 archetypeGroupId 为 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId为 jersey-quickstart-grizzly2 的原型,创建REST服务项目,使用
    IDEA
    创建项目如下:

    点击
    OK
    后,使用该原始模型创建项目。

  • 运行服务

    项目创建好后,原始模型已经默认创建了一个REST服务,我们可以直接启动REST服务,进入项目的根目录,执行如下命令构建和启动服务:

    mvn
    package

    mvn
    exec:java

    会启动REST服务,可以随时通过回车键停止服务,输出如下:

    六月 19, 2017 11:12:23 下午 org.glassfish.grizzly.http.server.NetworkListener start

    信息: Started listener bound to [localhost:8080]

    六月 19, 2017 11:12:23 下午 org.glassfish.grizzly.http.server.HttpServer start

    信息: [HttpServer] Started.

    Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl

    Hit enter to stop it…

    还提供了
    WADL,通过访问
    application.wadl
    可以获取当前REST服务公布的接口:

            <resources
    base="http://localhost:8080/myapp/">

                    <resource
    path="myresource">

                            <method
    id="getIt"
    name="GET">

                                    <response>

                                            <representation
    mediaType="text/plain"/>

                                    </response>

                            </method>

                    </resource>

            </resources>

  • 访问服务

    可以直接访问
    http://localhost:8080/myapp/myresource
    就可以访问REST服务,直接访问REST服务,会输出 Got it! 。

  • 项目说明

    启动服务的命令
    mvn
    exec:java,该命令实际调用了
    exec-maven-plugin 插件定义的一个值为 java 的 goal ,用以触发mainClass中的main函数,插件配置如下:

    <plugin>

              <groupId>org.codehaus.mojo</groupId>

              <artifactId>exec-maven-plugin</artifactId>

              <version>1.2.1</version>

              <executions>

                            <execution>

                                        <goals>

                                                <goal>java</goal>

                                      </goals>

                            </execution>

                </executions>

                <configuration>

                          <mainClass>org.drsoft.rest.Main</mainClass>

                </configuration>

      </plugin>

    REST服务类为
    MyResource,其
    @Path 中定义了资源路径,@GET中定义了GET方法getIt(),@Produces中定义了响应的类型为普通字符串,示例代码如下:

    @Path("myresource")

    public class MyResource {

     
     

            @GET

            @Produces(MediaType.TEXT_PLAIN)

            public String getIt() {

                    return
    "Got it!";

            }

    }

    REST服务的单元测试类MyResourceTest,在单元测试类中,在执行单元测试前需要启动服务,并使用Jersey
    Client中定义的方法来调用REST服务,示例代码如下:

    public class MyResourceTest {

            private HttpServer server;

            private WebTarget target;

            @Before

            public
    void
    setUp() throws Exception {

                    // start the server

                    server = Main.startServer();

                    // create the client

                    Client c = ClientBuilder.newClient();

     
     

                    // uncomment the following line if you want to enable

                    // support for JSON in the client (you also have to uncomment

                    // dependency on jersey-media-json module in pom.xml and Main.startServer())

                    // --

                    // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());

     
     

                    target = c.target(Main.BASE_URI);

            }

     
     

            @After

            public
    void
    tearDown() throws Exception {

                    server.stop();

            }

     
     

            @Test

            public
    void
    testGetIt() {

                    String responseMsg = target.path("myresource").request().get(String.class);

                    assertEquals("Got it!", responseMsg);

            }

    }

  • 基于Servlet容器服务
  • 创建项目

    我们首选使用 archetypeGroupId 为 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId为 jersey-quickstart-webapp
    的原型,创建REST服务项目,使用
    IDEA
    创建项目如下:

  • 运行服务

    由于这个是Web项目,没有main函数,因此必须部署到Servlet容器中,才能将其运行,我们需要配置Tomcat,IDEA的配置如下:

    • 点击
      Run菜单的
      Edit
      Configuration,在打开的窗体中增加
      Tomcat
      服务配置,指定Tomcat
      的安装目录,并设置当前站点的部署的虚拟目录名称,如下:

       
       

      点击OK后,就配置好Servlet容器,可以运行服务了

  • 访问服务

    服务启动后,我们可以访问
    http://localhost:8080/RESTWebAPP/webapi/myresource
    来调用REST服务,会输出
    Got it!

  • 项目说明

    Web根目录的名称为webapp,默认的Servlet容器版本为2.5,并且配置了WEB-INF/web.xml文件来配置REST服务,web.xml配置如下:

    <?xml
    version="1.0"
    encoding="UTF-8"?>

    <!-- This web.xml file is not required when using Servlet 3.0 container,

    see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->

    <web-app
    version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

            <servlet>

                    <servlet-name>Jersey Web Application</servlet-name>

                    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

                    <init-param>

                            <param-name>jersey.config.server.provider.packages</param-name>

                            <param-value>org.drsoft.rest</param-value>

                    </init-param>

                    <load-on-startup>1</load-on-startup>

            </servlet>

            <servlet-mapping>

                    <servlet-name>Jersey Web Application</servlet-name>

                    <url-pattern>/webapi/*</url-pattern>

            </servlet-mapping>

    </web-app>

     
     

笔记:创建Jersey REST 服务,基于Maven的更多相关文章

  1. idea创建Web项目(基于Maven多模块)

    简述:通常我们开发的项目结构是由多个modules项目组合而成,并且由有个parent的maven项目整体管理.废话少说,直接进入创建过程. 创建parent项目 1.打开idea工具,按照下图操作, ...

  2. MAC系统下用Idea创建spring boot工程 基于maven

    1.创建项目 打开idea编辑器,选择file  -> new -> project 点击next 依次填入group,artifact 填写完成之后再点击“next” 根据自己的需求在最 ...

  3. AMQ学习笔记 - 15. 实践方案:基于ActiveMQ的统一日志服务

    概述 以ActiveMQ + Log4j + Spring的技术组合,实现基于消息队列的统一日志服务. 参考:Spring+Log4j+ActiveMQ实现远程记录日志——实战+分析 与参考文章的比较 ...

  4. Jersey 2.x 从Maven Archetype 创建一个新项目

    创建 Jersey 工程需要使用 Apache 的 Maven 软件工程和管理工具.所有的Jersey产品模块都可以在 Maven中央库 中找到.这样的话 Jersey 可以非常容易和其他基于 Mav ...

  5. 基于maven使用IDEA创建多模块项目

    原文地址:http://blog.csdn.net/williamhappy/article/details/54376855 鉴于最近学习一个分布式项目的开发,讲一下关于使用IntelliJ IDE ...

  6. Jersey入门一:从Maven Archetype创建jersey项目

    1.用Ctrl+空格调出Spotlight搜索,输入ter调出终端窗口  2.在终端窗口进入将创建jersey项目的目录:  3.输入如下命令,创建一个名为的simple-service项目: m ...

  7. 基于maven的项目脚手架,一键创建项目的项目模板

    制作基于maven的项目脚手架 Springboot的出现极大的简化了项目开发的配置,然而,到真实使用的时候还是会有一堆配置需要设定.比如依赖管理,各种插件,质量扫描配置,docker配置,持续集成配 ...

  8. 使用python2与python3创建一个简单的http服务(基于SimpleHTTPServer)

    python2与python3基于SimpleHTTPServer创建一个http服务的方法是不同的: 一.在linux服务器上面检查一下自己的python版本:如: [root@zabbix ~]# ...

  9. (三)创建基于maven的javaFX+springboot项目创建

    创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...

随机推荐

  1. JAVA多线程与并发学习总结

    1.      计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...

  2. ubuntu网络设置及遇到问题

    1.在ubuntu下面显示有线网络设备未托管 解决:在ubuntu下面输入:sudo  gedit   /etc/NetworkManager/nm-system-settings.conf然后将里面 ...

  3. 分布式mysql中间件(mycat)

    1.   MyCAT概述 1.1 背景 随着传统的数据库技术日趋成熟.计算机网络技术的飞速发展和应用范围的扩充,数据库应用已经普遍建立于计算机网络之上.这时集中式数据库系统表现出它的不足: (1)集中 ...

  4. mongodb3.0分片及java代码连接操作测试(开启用户验证)

    最近抽时间搭建了一下mongodb简单的分片,整个过程还算是蛮顺利,只不过在用户验证这一块遇到了一些问题,好在最后终于搞定. 一.服务器搭建过程: 1.安装四个mongodb:一个作为config.一 ...

  5. linux下编译sphinx拓展

    编译libsphinxclient sphinx 源码包里的api文件夹下的libsphinxclient cd /root/api/libsphinxclient/ ./configure make ...

  6. An internal error occurred during: "Requesting JavaScript AST from selection". GC overhead limit exc

    1.错误描述 An internal error occurred during: "Requesting JavaScript AST from selection".     ...

  7. java.util.zip.ZipException:ZIP file must have at least one entry

    1.错误描述 java.util.zip.ZipException:ZIP file must have at least one entry 2.错误原因 由于在导出文件时,要将导出的文件压缩到压缩 ...

  8. JavaScript遍历table

    JavaScript遍历table 1.说明      遍历表格中的某行某列,并打印其值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  9. Linux以GB显示内存大小

    Linux以GB显示内存大小 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -g total used free shared buffers ca ...

  10. Linux显示服务器完整的状态信息

    Linux显示服务器完整的状态信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ apachectl [fullstatus] Usage: /usr/sbi ...