【Jersey】基于Jersey构建Restful Web应用

【Jersey】基于Jersey构建Restful Web应用

【Jersey】基于Jersey构建Restful Web应用

环境说明

  • java: 1.6;
  • tomcat: 6.0.48;
  • Jersey:1.18;

Jersey介绍

主要用于构建基于Restful的Web程序;

构建基于Maven的Javaweb程序


说明:
Javaweb项目是作为maven模块项目进行创建的,这里仅给出相应的pom文件,具体的创建过程可google搜索;
Javaweb引用了Dependencies模块(该模块主要负责引入各种maven的Jar)

项目结构图:
【Jersey】基于Jersey构建Restful Web应用
【Jersey】基于Jersey构建Restful Web应用

主项目pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.ll</groupId>
  6. <artifactId>MyStudyProject</artifactId>
  7. <version>1.0.0</version>
  8. <packaging>pom</packaging>
  9. <distributionManagement>
  10. <site>
  11. <id>website</id>
  12. <url>scp://webhost.company.com/www/website</url>
  13. </site>
  14. </distributionManagement>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. </properties>
  18. <modules>
  19. <module>MapReduce</module>
  20. <module>Lucene</module>
  21. <module>Dependencies</module>
  22. <module>DesignPattern</module>
  23. <module>Javaweb</module>
  24. </modules>
  25. </project>

Javaweb模块pom

【Jersey】基于Jersey构建Restful Web应用

Maven依赖

Spring相关Jar

  1. <properties>
  2. <springVersion>3.2.13.RELEASE</springVersion>
  3. </properties>
  4. <!-- Spring start -->
  5. <dependency>
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-aop</artifactId>
  8. <version>${springVersion}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-aspects</artifactId>
  13. <version>${springVersion}</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-beans</artifactId>
  18. <version>${springVersion}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. <version>${springVersion}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-context-support</artifactId>
  28. <version>${springVersion}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-core</artifactId>
  33. <version>${springVersion}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-jdbc</artifactId>
  38. <version>${springVersion}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-orm</artifactId>
  43. <version>${springVersion}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-tx</artifactId>
  48. <version>${springVersion}</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework</groupId>
  52. <artifactId>spring-jms</artifactId>
  53. <version>${springVersion}</version>
  54. </dependency
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-web</artifactId>
  58. <version>${springVersion}</version>
  59. </dependency
  60. <dependency>
  61. <groupId>org.springframework</groupId>
  62. <artifactId>spring-webmvc</artifactId>
  63. <version>${springVersion}</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-test</artifactId>
  68. <version>${springVersion}</version>
  69. <type>jar</type>
  70. <scope>compile</scope>
  71. </dependency>
  72. <!-- spring end -->

Jersey相关Jar

  1. <properties>
  2. <Dependency.version>1.0.0</Dependency.version>
  3. <Jersey.version>1.18</Jersey.version>
  4. </properties>
  5. <!-- Jersey start -->
  6. <dependency>
  7. <groupId>com.sun.jersey</groupId>
  8. <artifactId>jersey-server</artifactId>
  9. <version>${Jersey.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.sun.jersey</groupId>
  13. <artifactId>jersey-json</artifactId>
  14. <version>${Jersey.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.sun.jersey</groupId>
  18. <artifactId>jersey-client</artifactId>
  19. <version>${Jersey.version}</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.sun.jersey.contribs</groupId>
  23. <artifactId>jersey-spring</artifactId>
  24. <version>${Jersey.version}</version>
  25. <exclusions>
  26. <exclusion>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring</artifactId>
  29. </exclusion>
  30. <exclusion>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-core</artifactId>
  33. </exclusion>
  34. <exclusion>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-beans</artifactId>
  37. </exclusion>
  38. <exclusion>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-web</artifactId>
  41. </exclusion>
  42. <exclusion>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-context</artifactId>
  45. </exclusion>
  46. <exclusion>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-aop</artifactId>
  49. </exclusion>
  50. </exclusions>
  51. </dependency>
  52. <!-- Jersey end -->
说明:
因为jersey-spring.jar依赖spring包,而项目中其他模块也使用了通用的Spring模块,所以为避免jar包冲突,这里使用<exclusion>标签将spring相关jar包过滤掉;


Web.xml

配置Spring相关属性

这里没用到,可以不进行配置;
  1. <!--Spring 文件路径 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:conf/beans/beans-*.xml</param-value>
  5. </context-param>
  6. <!-- spring上下文监听器 -->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <listener>
  11. <listener-class>org.springframework.web.context.request.RequestContextListener<listener-class>
  12. </listener>

Jersey配置Servlet

  1. <!-- jersey Servlet -->
  2. <servlet>
  3. <servlet-name>rest-inside</servlet-name>
  4. <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  5. <init-param>
  6. <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  7. <param-value>true</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>com.sun.jersey.config.property.packages</param-name>
  11. <param-value>com.ll.rest</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>rest-inside</servlet-name>
  17. <url-pattern>/i/rest/*</url-pattern>
  18. </servlet-mapping>
配置说明:
  • 使用SpringServlet;
  • POJOMappingFeature: 用于Json和Pojo对象的转换;
  • com.sun.jersey.config.property.packages: 代表服务器端路径;

完整的Web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  4. version="2.4">
  5. <display-name>Java Web</display-name>
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <!--Spring 文件路径 -->
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:conf/beans/beans-*.xml</param-value>
  13. </context-param>
  14. <!-- spring上下文监听器 -->
  15. <listener>
  16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17. </listener>
  18. <listener>
  19. <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  20. </listener>
  21. <!-- 解决中文乱码 -->
  22. <filter>
  23. <filter-name>SetCharacterEncoding</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>UTF-8</param-value>
  28. </init-param>
  29. <init-param>
  30. <param-name>forceEncoding</param-name>
  31. <param-value>false</param-value>
  32. </init-param>
  33. </filter>
  34. <filter-mapping>
  35. <filter-name>SetCharacterEncoding</filter-name>
  36. <url-pattern>/*</url-pattern>
  37. </filter-mapping>
  38. <!-- jersey Servlet -->
  39. <servlet>
  40. <servlet-name>rest-inside</servlet-name>
  41. <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  42. <init-param>
  43. <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  44. <param-value>true</param-value>
  45. </init-param>
  46. <init-param>
  47. <param-name>com.sun.jersey.config.property.packages</param-name>
  48. <param-value>com.ll.rest</param-value>
  49. </init-param>
  50. <load-on-startup>1</load-on-startup>
  51. </servlet>
  52. <servlet-mapping>
  53. <servlet-name>rest-inside</servlet-name>
  54. <url-pattern>/i/rest/*</url-pattern>
  55. </servlet-mapping>
  56. </web-app>

Jersey服务端代码

【Jersey】基于Jersey构建Restful Web应用
代码如下:
  1. package com.ll.rest;
  2. import javax.ws.rs.Consumes;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.PathParam;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.MediaType;
  8. import com.ll.model.Student;
  9. @Path("/student")
  10. public class JerseyServerTest {
  11. @Path("get/{name}")
  12. @GET
  13. @Produces(MediaType.APPLICATION_JSON)
  14. @Consumes(MediaType.APPLICATION_JSON)
  15. public Student status(@PathParam("name") String name) {
  16. System.out.println("get Student, name: "+name);
  17. Student student = new Student();
  18. student.setAge(25);
  19. student.setName(name);
  20. student.setSchool("SuZhou");
  21. return student;
  22. }
  23. }

Student.java代码如下:
【Jersey】基于Jersey构建Restful Web应用



安装tomcat,并部署Javaweb

从官网下载tomcat后,解压缩,配置好环境变量即可使用,这里不做详述;

配置Javaweb为tomcat默认项目

tomcat默认项目位于Root下,为了让我们的Javaweb成为默认项目,需要修改配置文件;
  • 配置文件名称:server.xml
修改如下内容:
【Jersey】基于Jersey构建Restful Web应用
  1. <Context path="" docBase="C:\xxx\Javaweb" debug="0" reloadable="true"/>

启动tomcat

说明:启动前需要对javaweb进行 mvn install(或package)
每一次修改javaweb相关程序,都需要先对javaweb进行重新编译;
【Jersey】基于Jersey构建Restful Web应用


测试Jersey

方式1: 使用postman插件测试(Chrome的一个插件,专门用于发送http请求的);
方式2: 编写Jersey的Client程序进行测试;

方式1: 使用postman插件测试

【Jersey】基于Jersey构建Restful Web应用


方式2: 编写Jersey的Client程序进行测试

客户端程序:
【Jersey】基于Jersey构建Restful Web应用
完整程序:
  1. import javax.ws.rs.core.MediaType;
  2. import javax.ws.rs.core.MultivaluedMap;
  3. import com.sun.jersey.api.client.Client;
  4. import com.sun.jersey.api.client.config.ClientConfig;
  5. import com.sun.jersey.api.client.config.DefaultClientConfig;
  6. import com.sun.jersey.api.json.JSONConfiguration;
  7. public class StudentDao {
  8. private Client client;
  9. public void init() {
  10. if (client == null) {
  11. ClientConfig config = new DefaultClientConfig();
  12. config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
  13. true);
  14. client = Client.create(config);
  15. client.setConnectTimeout(3000);
  16. }
  17. }
  18. public Student get(String url, String path,
  19. MultivaluedMap<String, String> queryParams) {
  20. try {
  21. return queryParams == null ? client.resource(completeUrl(url))
  22. .path(path)
  23. .type(MediaType.APPLICATION_JSON)
  24. .get(Student.class)
  25. : client.resource(completeUrl(url))
  26. .queryParams(queryParams)
  27. .path(path)
  28. .type(MediaType.APPLICATION_JSON)
  29. .get(Student.class);
  30. } catch (Exception e) {
  31. return null;
  32. }
  33. }
  34. private String completeUrl(String url) {
  35. url = url.toLowerCase();
  36. if (url.startsWith("http://")) {
  37. return url;
  38. } else {
  39. return "http://" + url;
  40. }
  41. }
  42. public static void main(String[] args) {
  43. StudentDao dao = new StudentDao();
  44. dao.init();
  45. Student ret = dao.get("http://127.0.0.1:8080/",
  46. "i/rest/student/get/Tom", null);
  47. System.out.println("请求返回:");
  48. System.out.println(ret);
  49. }
  50. }
测试结果:
【Jersey】基于Jersey构建Restful Web应用



上一篇:Linux面试基础题-2


下一篇:linux面试题目--1