我的关注 |
¥0 在Eclipse中如何用JAX-RS和Jersey部署Restful服务?
0

在Eclipse中如何用JAX-RS和Jersey部署Restful服务?

hong
修改
评论(0)
1个回答
0
采纳
  1. Eclipse=>File => New => Dynamic Web Project. => 给项目命名为”TestExample” => next => next => 勾选”Gnerate web.xml deployment descriptor”
  2. 在TestExample项目上右击 -> Configure -> Convert to Maven Project. -> 填写Group id和Artiact id
  3. 修改pom.xml,添加依赖库。
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>TestExample注意修改这里</groupId>
    5. <artifactId>TestExample注意修改这里</artifactId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. <packaging>war</packaging>
    8. <build>
    9. <sourceDirectory>src</sourceDirectory>
    10. <plugins>
    11. <plugin>
    12. <artifactId>maven-compiler-plugin</artifactId>
    13. <version>3.5.1</version>
    14. <configuration>
    15. <source>1.8</source>
    16. <target>1.8</target>
    17. </configuration>
    18. </plugin>
    19. <plugin>
    20. <artifactId>maven-war-plugin</artifactId>
    21. <version>2.6</version>
    22. <configuration>
    23. <warSourceDirectory>WebContent</warSourceDirectory>
    24. <failOnMissingWebXml>false</failOnMissingWebXml>
    25. </configuration>
    26. </plugin>
    27. </plugins>
    28. </build>
    29. <dependencies>
    30. <dependency>
    31. <groupId>asm</groupId>
    32. <artifactId>asm</artifactId>
    33. <version>[3.3.1,)</version>
    34. </dependency>
    35. <dependency>
    36. <groupId>com.sun.jersey</groupId>
    37. <artifactId>jersey-bundle</artifactId>
    38. <version>[1.19,)</version>
    39. </dependency>
    40. <dependency>
    41. <groupId>org.json</groupId>
    42. <artifactId>json</artifactId>
    43. <version>[20140107,)</version>
    44. </dependency>
    45. <dependency>
    46. <groupId>com.sun.jersey</groupId>
    47. <artifactId>jersey-server</artifactId>
    48. <version>[1.4,)</version>
    49. </dependency>
    50. <dependency>
    51. <groupId>com.sun.jersey</groupId>
    52. <artifactId>jersey-core</artifactId>
    53. <version>[1.4,)</version>
    54. </dependency>
    55. <dependency>
    56. <groupId>mysql</groupId>
    57. <artifactId>mysql-connector-java</artifactId>
    58. <version>5.1.40</version>
    59. </dependency>
    60. <dependency>
    61. <groupId>redis.clients</groupId>
    62. <artifactId>jedis</artifactId>
    63. <version>2.8.2</version>
    64. </dependency>
    65. <dependency>
    66. <groupId>javax.mail</groupId>
    67. <artifactId>mail</artifactId>
    68. <version>1.4.7</version>
    69. </dependency>
    70. </dependencies>
    71. </project>
  4. 修改web.xml文件,添加如下:

    1. <servlet>
    2. <servlet-name>Jersey Web Application</servlet-name>
    3. <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    4. <load-on-startup>1</load-on-startup>
    5. </servlet>
    6. <servlet-mapping>
    7. <servlet-name>Jersey Web Application</servlet-name>
    8. <url-pattern>/service/*</url-pattern>
    9. </servlet-mapping>
  5. 添加test.Exapmle java类

    1. package test;
    2. import javax.ws.rs.GET;
    3. import javax.ws.rs.Path;
    4. import javax.ws.rs.Produces;
    5. @Path("/test")
    6. public class Example {
    7. @GET
    8. @Path("/hello")
    9. @Produces("application/json")
    10. public String fn() {
    11. return "hello world";
    12. }
    13. }
  6. clean and build project

    1. 1. Project -> Clean
    2. 2. Project -> Right click -> Maven -> Update Project
    3. 3. Project -> Right click -> Run As.. -> Maven Build (option 5) -> Add "clean install" -> Run
  7. 将项目在tomcat上运行
    在项目上右击-> Run as -> Run on Server

  8. 在浏览器里输入:
    localhost:8080/TestExample/service/test/hello
    输出hello world

采纳答案
大智若愚
修改
评论 (0)
撰写回答