这是一个Hello World例子如何使用Jersey构造一个Web Application
使用工具
1. Apache Tomcat 7.x
1. Eclipse Java EE IDE for Web Developers; Version: Neon.2 Release
2. Jersey库
(比较方便的构造Jersey应用是使用maven生产框架,但是由于国内访问maven库的限制,不得采用直接下载Jersey库的办法)
Step 1: 下载和安装Apache Tomcat 7.x(不细说了)
Step 3: 下载Jersey库(https://jersey.java.net/download.html),包括
- Jersey JAX-RS 2.0 RI bundle
- Jersey MVC
把所有用到的库拷贝到WEB-INF/lib目录下面:
Step 2: 使用Eclipse创建一个"Dynamic Web Project"
2.1 创建Eclipse工程
选择缺省module,并且选中缺省web.xml
2.2 创建Application类
package com.jersey.sample.application;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
public class MyApplication extends ResourceConfig{
public MyApplication(){
packages("com.jersey.sample.controller");
register(JspMvcFeature.class);
property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}
}
2.3 创建Controller类
package com.jersey.sample.controller;
import java.util.HashMap;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import org.glassfish.jersey.server.mvc.Viewable;
@Path("/hello")
public class HelloController {
@GET
public Viewable sayHello(@QueryParam("name") @DefaultValue("World") String name) {
HashMap<String, Object> model = new HashMap<String, Object>();
model.put("name", name);
return new Viewable("/hello", model);
}
@GET
@Path("/{param}")
@Produces("text/plain;charset=UTF-8")
public String sayHelloToUTF8(@PathParam("param") String username) {
return "Hello " + username;
}
}
2.4 创建WEB-INF/jsp/hello.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
</head>
<body>
Hello, <c:out value="${it.name}" />
</body>
</html>
2.5 编辑web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JerseySample</display-name>
<!-- Jersey -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.jersey.sample.application.MyApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. 运行
- http://localhost:8080/JerseySample/hello
Hello, World
- http://localhost:8080/JerseySample/hello?name=aaaa
Hello, aaaa
- http://localhost:8080/JerseySample/hello/bbb
Hello bbb