您的位置:首页 > 其它

JERSEY中文翻译(第一章、Getting Started、1.1.7)

2013-08-17 11:45 344 查看
最近发现jersey特别流行,但是中文资料非常少,深感没有资料的痛苦,所以分享一下看到的内容供他人快速入门。

今天翻译第一章、Getting Started。https://jersey.java.net/documentation/1.17/user-guide.html#getting-started

第一章、Getting Started、1.1.7

本章将会告诉你如何使用嵌入式的Grizzly server创建一个Jersey服务,在本章的最后一节也将展现如何使用一段程序来建立一个web应用。

首先它需要依赖jersey的jar包。使用Maven开发,需要设置:jersey-serverjersey-grizzly2 两个包,在pom.xml内添加:

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


如果你需要依赖于jersey的快照版本,那么在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>


1.1 创建一个资源

创建一个java文件

1     // The Java class will be hosted at the URI path "/helloworld"
2     @Path("/helloworld")
3     public class HelloWorldResource {
4
5         // The Java method will process HTTP GET requests
6         @GET
7         // The Java method will produce content identified by the MIME Media
8         // type "text/plain"
9         @Produces("text/plain")
10         public String getClichedMessage() {
11             // Return some cliched textual content
12             return "Hello World";
13         }
14     }


这个一个简单的web应用,URI路径是"/helloword"(第二行);它支持HTTP的GET方式请求(第六行);返回的相应头是"text/plain"(第九行);返回内容是"Hello World"(第十二行)

1.2 部署一个WEB应用

这个程序部署在Grizzle容器内。在你的工程内创建一个java程序

1 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
2 import com.sun.jersey.api.core.DefaultResourceConfig;
3 import com.sun.jersey.api.core.PackagesResourceConfig;
4 import com.sun.jersey.api.core.ResourceConfig;
5 import org.glassfish.grizzly.http.server.HttpServer;
6
7 import javax.ws.rs.core.UriBuilder;
8 import java.io.IOException;
9 import java.net.URI;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 public class Main {
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.sun.jersey.samples.helloworld.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 }


用这个主函数,承接Grizzle容器,实现HelloWorldResource。

在24行,设置了当Jersey去哪里查找部署的资源class

在25行,设置了基本的路径和端口号"localhost:9998",并且返回一个Grizzle的HttpServer。全部的Hello World资源路径是"http://localhost:9998/helloworld"

1.3 测试这个资源

直接访问"http://localhost:9998/helloworld"或者curl "localhost:9998/helloworld"

1.4 事例

创建一个java web程序,HelloWorld-WebApp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: