环境说明
- java: 1.6;
- tomcat: 6.0.48;
- Jersey:1.18;
Jersey介绍
主要用于构建基于Restful的Web程序;
构建基于Maven的Javaweb程序
说明:
Javaweb项目是作为maven模块项目进行创建的,这里仅给出相应的pom文件,具体的创建过程可google搜索;
Javaweb引用了Dependencies模块(该模块主要负责引入各种maven的Jar)
项目结构图:
主项目pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ll</groupId>
<artifactId>MyStudyProject</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<distributionManagement>
<site>
<id>website</id>
<url>scp://webhost.company.com/www/website</url>
</site>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>MapReduce</module>
<module>Lucene</module>
<module>Dependencies</module>
<module>DesignPattern</module>
<module>Javaweb</module>
</modules>
</project>
Javaweb模块pom
Maven依赖
Spring相关Jar
<properties>
<springVersion>3.2.13.RELEASE</springVersion>
</properties>
<!-- Spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${springVersion}</version>
</dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springVersion}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- spring end -->
Jersey相关Jar
<properties>
<Dependency.version>1.0.0</Dependency.version>
<Jersey.version>1.18</Jersey.version>
</properties>
<!-- Jersey start -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${Jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${Jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${Jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${Jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Jersey end -->
说明:
因为jersey-spring.jar依赖spring包,而项目中其他模块也使用了通用的Spring模块,所以为避免jar包冲突,这里使用<exclusion>标签将spring相关jar包过滤掉;
Web.xml
配置Spring相关属性
这里没用到,可以不进行配置;
<!--Spring 文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/beans/beans-*.xml</param-value>
</context-param>
<!-- spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener<listener-class>
</listener>
Jersey配置Servlet
<!-- jersey Servlet -->
<servlet>
<servlet-name>rest-inside</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ll.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-inside</servlet-name>
<url-pattern>/i/rest/*</url-pattern>
</servlet-mapping>
配置说明:
- 使用SpringServlet;
- POJOMappingFeature: 用于Json和Pojo对象的转换;
- com.sun.jersey.config.property.packages: 代表服务器端路径;
完整的Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Java Web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Spring 文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/beans/beans-*.xml</param-value>
</context-param>
<!-- spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 解决中文乱码 -->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- jersey Servlet -->
<servlet>
<servlet-name>rest-inside</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ll.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-inside</servlet-name>
<url-pattern>/i/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Jersey服务端代码
代码如下:
package com.ll.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.ll.model.Student;
@Path("/student")
public class JerseyServerTest {
@Path("get/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Student status(@PathParam("name") String name) {
System.out.println("get Student, name: "+name);
Student student = new Student();
student.setAge(25);
student.setName(name);
student.setSchool("SuZhou");
return student;
}
}
Student.java代码如下:
安装tomcat,并部署Javaweb
从官网下载tomcat后,解压缩,配置好环境变量即可使用,这里不做详述;
配置Javaweb为tomcat默认项目
tomcat默认项目位于Root下,为了让我们的Javaweb成为默认项目,需要修改配置文件;
- 配置文件名称:server.xml
修改如下内容:
<Context path="" docBase="C:\xxx\Javaweb" debug="0" reloadable="true"/>
启动tomcat
说明:启动前需要对javaweb进行 mvn install(或package)
每一次修改javaweb相关程序,都需要先对javaweb进行重新编译;
测试Jersey
方式1: 使用postman插件测试(Chrome的一个插件,专门用于发送http请求的);
方式2: 编写Jersey的Client程序进行测试;
方式1: 使用postman插件测试
方式2: 编写Jersey的Client程序进行测试
客户端程序:
完整程序:
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
public class StudentDao {
private Client client;
public void init() {
if (client == null) {
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
true);
client = Client.create(config);
client.setConnectTimeout(3000);
}
}
public Student get(String url, String path,
MultivaluedMap<String, String> queryParams) {
try {
return queryParams == null ? client.resource(completeUrl(url))
.path(path)
.type(MediaType.APPLICATION_JSON)
.get(Student.class)
: client.resource(completeUrl(url))
.queryParams(queryParams)
.path(path)
.type(MediaType.APPLICATION_JSON)
.get(Student.class);
} catch (Exception e) {
return null;
}
}
private String completeUrl(String url) {
url = url.toLowerCase();
if (url.startsWith("http://")) {
return url;
} else {
return "http://" + url;
}
}
public static void main(String[] args) {
StudentDao dao = new StudentDao();
dao.init();
Student ret = dao.get("http://127.0.0.1:8080/",
"i/rest/student/get/Tom", null);
System.out.println("请求返回:");
System.out.println(ret);
}
}
测试结果: