HoneyMoose
  • 首页
  • Java
  • Jersey
  • Jira
  • Confluence
  • U.S.
    • Real Estate
    • U.S. Travel
    • H1B
  • 项目和网站
    • CWIKI.US
    • OSSEZ 社区
    • WIKI.OSSEZ.COM
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
Jersey
Jersey
Jersey

Jersey 2.x 运行项目

现在我们已经有可以可以运行的项目了,让我们队这个项目进行一些测试吧。 你需要运行下面的一些命令行: mvn clean test 这个命令将会对项目进行编译后运行单元测试。 你应该会看到和下面类似的输出表示项目编译成功了: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.742 s [INFO] Finished at: 2018-03-27T15:05:50-04:00 [INFO] Final Memory: 17M/205M [INFO] ------------------------------------------------------------------------ 上面的输出表示的是项目已经被编译测试通过了。 我们可以开始使用独立启动方式启动项目了,希望直接启动项目,需要运行下面的 mvn 项目启动命令: mvn exec:java 这时候,项目应该已经正常启动了,很快你应该可以在控制台上看到下面的输出: Mar 27, 2018 3:10:28 PM org.glassfish.grizzly.http.server.NetworkListener start INFO: Started listener bound to [localhost:8080] Mar 27, 2018 3:10:28 PM org.glassfish.grizzly.http.server.HttpServer start INFO: [HttpServer] Started. Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl Hit enter to stop it... 项目这个时候已经运行了,有关项目的 WADL 描述文件可以通过 http://localhost:8080/myapp/application.wadl URL 访问到。 你可以考虑在你的控制台中通过命令中 curl http://localhost:8080/myapp/application.wadl 访问这个项目的描述文件,你也可以直接将这个链接拷贝粘贴到浏览器中直接进行查看。 如果一切正常的话,你应该可以看到你部署的 RESTful 应用程序的的 WADL 格式的 XML 文档。希望查看更多有关 WADL 的内容,请查看章节  Chapter 17, WADL Support。 部署成功后最后一件可以尝试的事情就是与部署的 /myresource 资源进行数据交互。 你可以考虑直接把  http://localhost:8080/myapp/myresource 链接复制粘贴到浏览器中,你也可以通过 curl 执行命令。 有关 curl 执行命令的结果如下: $ curl http://localhost:8080/myapp/myresource Got it! 你可以看到,当你执行上面的命令后,控制台输出了 Got it!消息,这个消息是服务器发送给我们的资源。 我们也可以通过参数 -i 让 curl 提供更多的信息给我们来让我们了解有关消息发送响应的相关信息。 curl -i http://localhost:8080/myapp/myresource HTTP/1.1 200 OK Content-Type: text/plain Date: Sun, 26 May 2013 18:27:19 GMT Content-Length: 7 Got it! 注意到Content-Type: text/plain是在 MyResource 类中用@Produces 注解的。 如果想看到更多返回信息,或者想了解 curl 客户端和运行的 Grizzly I/O 容器的交互,可以变换不同的 curl 命令参数。例如下面的例子可以让 curl 客户端输出更多有关于服务器通信和交互的相关信息: $ curl -v http://localhost:8080/myapp/myresource * About to connect() to localhost port 8080 (#0) * Trying ::1... * Connection refused * Trying 127.0.0.1... * connected * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /myapp/myresource HTTP/1.1 > User-Agent: curl/7.25.0 (x86_64-apple-darwin11.3.0) libcurl/7.25.0 OpenSSL/1.0.1e zlib/1.2.7 libidn/1.22 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: text/plain < Date: Sun, 26 May 2013 18:29:18 GMT < Content-Length: 7 < * Connection #0 to host localhost left intact Got it!* Closing connection #0 https://www.cwiki.us/display/JERSEYZH/Running+the+Project

2018年11月08日 0Comments 352Browse 0Like Read more
Jersey

Jersey 2.x 探索新建的工程

如果用 Jersey maven archetype 成功创建了这个项目,那么在你当前的路径下就已经创建了一个名为simple-service项目。它包含了一个标准的Maven项目结构: 说明 文件目录 在项目根目录下的的项目构建和管理配置描述文件  pom.xml 项目源代码文件 src/main/java 项目测试源代码 src/test/java 在原文路径下的com.example包中有两个 class 文件,这个 Main 类主要是负责启动 Grizzly 容器,同时也为这个容器配置和部署 JAX-RS 应用。 在同一个包内的另外一个类 MyResource 类是 JAX-RS 的一个实现的源代码,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.example;   import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;   /**  * Root resource (exposed at "myresource" path)  */ @Path("myresource") public class MyResource {       /**      * Method handling HTTP GET requests. The returned object will be sent      * to the client as "text/plain" media type.      *      * @return String that will be returned as a text/plain response.      */     @GET     @Produces(MediaType.TEXT_PLAIN)     public String getIt() {         return "Got it!";     } } 一个 JAX-RS 资源是一个可以处理绑定了资源的URI的HTTP请求的带有注解的POJO,详细内容可以看第三章。在我们的例子中,单一的资源暴露了一个公开的方法,能够处理HTTP GET请求,绑定在/myresourceURI路径下,可以产生媒体类型为“text/plain”的响应消息。在这个示例中,资源返回相同的“Got it!”应对所有客户端的要求。 在src/test/java目录下的 MyResourceTest 类是对 MyResource 的单元测试,他们具有相同的包 com.example 中。但是这个单元测试类被放置到 maven 工程中的测试源代码目录下 src/test/java(为了增加代码的简洁性,一些代码的注释和 JUnit 的导入类被省略了)。 package com.example;   import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget;   import org.glassfish.grizzly.http.server.HttpServer;   ...   public class MyResourceTest {       private HttpServer server;     private WebTarget target;       @Before     public void setUp() throws Exception {         server = Main.startServer();           Client c = ClientBuilder.newClient();         target = c.target(Main.BASE_URI);     }       @After     public void tearDown() throws Exception {         server.stop();     }       /**      * Test to see that the message "Got it!" is sent in the response.      */     @Test     public void testGetIt() {         String responseMsg = target.path("myresource").request().get(String.class);         assertEquals("Got it!", responseMsg);     } } 在这个单元测试中静态方法 Main.startServer()首先将 Grizzly 容器启动,然后服务器应用部署到测试中的 setUp() 方法。接下来,一个JAX-RS 客户端组件在相同的测试方法创建。 先是一个新的JAX-RS客户端实例生成,接着JAX-RS web target 部件指向我们部署的应用程序上下文的 root:http://localhost:8080/myapp/( Main.BASE_URI 的常量值)存储在单元测试类目标区。这个区被用于实际的单元测试方法(testgetit())。 在 testgetit() 方法中,JAX-RS 客户端 API 是用来连接并发送 HTTP GET 请求的 MyResource JAX-RS 资源类侦听在/myresource 的URI。同样作为 JAX-RS API 方法调用链的一部分,回应以Java字符串类型被读到。在测试方法的第二行,响应的内容(从服务器返回的字符串)跟测试断言预期短语比较。要了解更多有关使用 JAX-RS 客户端API,请参阅第5章客户端API。 https://www.cwiki.us/display/JERSEYZH/Exploring+the+Newly+Created+Project

2018年11月08日 0Comments 347Browse 0Like Read more
Jersey

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

创建 Jersey 工程需要使用 Apache 的 Maven 软件工程和管理工具。所有的Jersey产品模块都可以在 Maven中央库 中找到。这样的话 Jersey 可以非常容易和其他基于 Maven 的项目进行配置(non-SNAPSHOT)。 Jersey 已经部署到中央仓库中了,因此你不需要配置其他仓库来让 Jersey 可以工作。 有关 SNAPSHOT 版本 如果你想要使用最新的 Jersey 模块的 SNAPSHOT 版本(SNAPSHOT 版本代表不稳定、尚处于开发中的版本),需要在 pom.xml 中添加如下内容: <repository>     <id>snapshot-repository.java.net</id>     <name>Java.net Snapshot Repository for Maven</name>     <url>https://maven.java.net/content/repositories/snapshots/</url>     <layout>default</layout> </repository> 使用 Maven 的工程创建一个 Jersey 项目是最方便的,让我们用这种方法来看一下它是怎么实现的。让我们创建一个新的 Jersey 项目,运行在Grizzly容器。 我们使用 Jersey-provided 的 maven archetype。创建一个项目,需要执行下面的代码: mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \ -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \ -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \ -DarchetypeVersion=2.26 在你的项目里面随意调整 pom.xml 内的 groupId,包名和版本号就可以成为一个新的项目。   https://www.cwiki.us/display/JERSEYZH/Creating+a+New+Project+from+Maven+Archetype

2018年11月08日 0Comments 341Browse 0Like Read more
Jersey

Jersey 2.x 服务器端应用支持的容器

基于 JAX-RS Servlet-based 部署的一部分标准,能运行在任何支持 Servlet 2.5 和更高标准的的容器上。Jersey 提供支持程序化部署在下面的容器中:Grizzly 2 (HTTP 和 Servlet), JDK Http server,Simple Http server 和 Jetty Http server 本章节仅仅提供了需要 Maven 提供的依赖,有关更多的细节,请参考 Chapter 4, Application Deployment and Runtime Environments 页面中的内容。 1 2 3 4 5 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 31 32 33 34 35 <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-grizzly2-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-grizzly2-servlet</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jdk-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-simple-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jetty-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jetty-servlet</artifactId>     <version>2.27</version> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 295Browse 0Like Read more
Jersey

Jersey 2.x JDK 上的客户端应用

如应用是运行在 JDK 上的话,你只需要使用 JAX-RS 中的客户端部分就可以了,这个根据你使用的客户端有所调整。 这里有一系列的模块是可以供你使用的,例如 grizzly 或 apache 或 jetty connector(请参考下面的的依赖表格)。Jersey 客户端默认使用 JDK 进行运行(使用的是 HttpUrlConnection)。 请参考 Chapter 5, Client API 获得更多的细节。 1 2 3 4 5 <dependency>     <groupId>org.glassfish.jersey.core</groupId>     <artifactId>jersey-client</artifactId>     <version>2.27</version> </dependency> 当前可用的连接器: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-grizzly-connector</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-apache-connector</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-jetty-connector</artifactId>     <version>2.27</version> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 345Browse 0Like Read more
Jersey

Jersey 2.x 基于 Servlet 的服务器端应用

下面的依赖通常应用到应用服务器上(servlet 容器),同时这个应用服务器上没有整合任何 JAX-RS 的实现。 因此,这个应用服务器需要包含有 JAX-RS API 和 Jersey 实现,同时部署到服务器上。 1 2 3 4 5 6 7 8 9 10 11 12 <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->     <artifactId>jersey-container-servlet</artifactId>     <version>2.27</version> </dependency> <!-- Required only when you are using JAX-RS Client --> <dependency>     <groupId>org.glassfish.jersey.core</groupId>     <artifactId>jersey-client</artifactId>     <version>2.27</version> </dependency>   https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 293Browse 0Like Read more
Jersey

Jersey 2.x Glassfish 中基于 Servlet 的应用

如果你使用的是 Glassfish 作为你应用服务器,你不需要在你的引用中包含引用任何东西,所有你需要的都已经包含进去了。 你只需要定义 JAX-RS API 以便于你能够对你的应用进行编辑,使用 (provided)依赖。 1 2 3 4 5 6 <dependency>     <groupId>javax.ws.rs</groupId>     <artifactId>javax.ws.rs-api</artifactId>     <version>2.1</version>     <scope>provided</scope> </dependency> 如果你需要使用 Jersey 的一些特定特性,你需要基于你的 Jersey 目录进行添加。 1 2 3 4 5 6 7 8 9 10 11 12 13 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.27</version> <scope>provided</scope> </dependency> <!-- if you are using Jersey client specific features without the server side --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.27</version> <scope>provided</scope> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月06日 0Comments 262Browse 0Like Read more
Jersey

Jersey 2.x 前言和约定的文本格式

这是Jersey 2.x 的用户指南。我们极力将它能与我们新增的功能保持一致。当阅读本指南,作为补充,也请移步至 Jersey API documentation 查看 Jersey 的特性和 API。 欢迎任何对本指南的建议和提问,可以联系[email protected],同样的,发现勘误,也可以在Jersey JIRA Issue Tracker 提问,请注意需要在 docs 文档组件部分中进行提问。 请注意,在提问的时候需要注明你有疑问的 Jersey 用户指南的版本,你在哪里发现的错误,同时请注明影响到的那个版本。 约定的文本格式 首先需要说明的是,任何 Jersey 和 JAX-RS API 的组件将会在参考组件中链接到 API 文档。任何组件下面的子组件将会使用非寸线字体。 Emphasised 字体被使用在第一次出现的介绍中。 在一些代码的列表中,有些代码太长而不能在一行中完整显示下来的话,代码行将会扩展到下一页或者下一段中,我们将会用  '\'  标识在每一行的页尾部用于表示这些内容应该是在一行中显示的。 例如: This is an overly long line that \ might not fit the available page \ width and had to be broken into \ multiple lines. This line fits the page width. (这一行的字符能够在一行中完整显示) 上面的内容应该阅读成下面的内容: This is an overly long line that might not fit the available page width and had to be broken into multiple lines. This line fits the page width. 换句话说如果每一行的尾部有斜杠,则表示的是需要将下一行的内容拼接到上以后来进行完整的阅读。   https://www.cwiki.us/display/JERSEYZH/Preface

2018年11月06日 0Comments 285Browse 0Like Read more
Jersey

介绍 Jersey 依赖

Jersey 使用 Apache Maven 进行构造和安装。非快照(Non-snapshot)Jersey 发行版本是不会部署到 中心 Maven 仓库(Central Maven Repository)。 Jersey 同样也会部署到 toJava.Net 中心仓库(Java.Net Maven repositories)中,在这个仓库中同时部署了 Jersey 的快照版本以便于你测试最新的 Jersey 安装版本,请参考 Java.Net Snapshots Maven repository 中的内容。 一个应用使用 Jersey 和依赖 Jersey 的模块,同时还包括了有 Jersey 使用的第三方模块,这些第三方的模块可能会被 Jersey 使用。Jersey 被设计为插件式的系统结构,不同的应用程序可以引用 Jersey 中的不同模块。这个同时也意味着一系列的外部依赖可能被使用基于每一个项目的不同而不同。 开发者在项目中使用 Maven 或者类似 Maven 的编译系统能够比较容易的找到需要的依赖的包,相对于使用其他的编译系统来说。这里将会对使用 Maven 和不使用 Maven 的开发者如何在项目中使用 Jersey 模块。 Ant 的开发者,请参考下面的文章: Ant Tasks for Maven。   https://www.cwiki.us/display/JERSEYZH/Introduction+to+Jersey+dependencies

2018年11月05日 0Comments 252Browse 0Like Read more
Jersey

Jersey 2.x 分支 Java SE 兼容性

直到 Jersey 2.6 版本,Jersey 将会兼容 Java SE 6。这个情况将会在在 Jersey 2.7 的版本有所改变。 直到 Jersey 版本 2.25x, 所有的 Jersey 的组件将会兼容 Java SE 7 为目标。这个意思是你将会需要最少使用 Java SE 7 来支持和运行使用最新的。只有 core-common 和 core-client 模块仍然兼容 Java SE 6 的版本并且可以在这个版本上运行。 从 Jersey 2.26 开始,所有的模块将会使用 Java SE 8 进行构建,这里并不能支持运行老的 Java SE 分发包。 https://www.cwiki.us/display/JERSEYZH/Java+SE+Compatibility

2018年11月05日 0Comments 309Browse 0Like Read more
12
Newest Hotspots Random
Newest Hotspots Random
Python 日期格式和时间以及当前时间和时间戳 Python DateTime 日期处理 Python With 关键字和语句 Python yaml 使用的包 Python 通过命令行安装包的时候 pip 提示错误 Python 注释
我该因为疫情而放弃美国绿卡吗 Confluence 6 获得 Active Directory 服务器证书 听说美国只有5%的高危城市会有人身安全问题,其它都没,是真的吗 为什么电影里美国人开的车都很一般 Confluence 6 附件存储提取文本文件 Confluence 6 workbox 通知包含了什么
Categories
  • Akka
  • Algorithm(算法)
  • AMQP
  • Angular
  • CI
  • Compile And CI
  • Computer Science
  • Confluence
  • DataBase
  • Gradle
  • H1B
  • Hibernate
  • IDE
  • Java
  • Jersey
  • Jira
  • MariaDB
  • PrestaShop
  • Real Estate
  • Spring
  • Spring Batch
  • U.S.
  • U.S. Travel
  • 我的小厨

COPYRIGHT © 2020 HoneyMoose. ALL RIGHTS RESERVED.

THEME CWIKIUS.CN MADE BY VTROIS