Jersey1.x+Tomcat 入门

http://blog.csdn.net/waltertan1988/article/details/45895359


1. 在Eclipse中新建一个Maven的Web项目,其中红色部分为要修改的文件


2. 在pom.xml中引入Jersey的依赖项:

[html]  view plain  copy
  1. <!-- 引入 Jersey的相关依赖项 -->  
  2. <dependency>  
  3.     <groupId>com.sun.jersey</groupId>  
  4.     <artifactId>jersey-server</artifactId>  
  5.     <version>1.19</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>com.sun.jersey</groupId>  
  9.     <artifactId>jersey-servlet</artifactId>  
  10.     <version>1.19</version>  
  11. </dependency>  


3. 编写RESTful的资源类HelloWorldResource.java

[java]  view plain  copy
  1. package restful.res;  
  2.   
  3.   
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.Path;  
  6. import javax.ws.rs.Produces;  
  7.   
  8. /** 
  9.  * The Java class will be hosted at the URI path "/helloworld" 
  10.  *  
  11.  * @author hp 
  12.  *  
  13.  */  
  14. @Path("/helloworld")  
  15. public class HelloWorldResource {  
  16.     // The Java method will process HTTP GET requests  
  17.     @GET  
  18.     // The Java method will produce content identified by the MIME Media  
  19.     // type "text/plain"  
  20.     @Produces("text/plain")  
  21.     public String getClichedMessage() {  
  22.         // Return some cliched textual content  
  23.         return "Jersey Hello World";  
  24.     }  
  25. }  

4. 在web.xml中添加对Jersey支持的Servlet:

[html]  view plain  copy
  1. <!-- 引入支持Jersey的Servlet容器 -->  
  2. <servlet>  
  3.     <servlet-name>JerseyFirstServlet</servlet-name>  
  4.     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
  5.     <init-param>  
  6.         <!-- 设置REST资源所在的包(可选) -->  
  7.         <param-name>com.sun.jersey.config.property.packages</param-name>  
  8.         <param-value>restful.res</param-value>  
  9.     </init-param>  
  10. </servlet>  
  11. <servlet-mapping>  
  12.     <servlet-name>JerseyFirstServlet</servlet-name>  
  13.     <url-pattern>/restful/*</url-pattern>  
  14. </servlet-mapping>  

5. 用Maven打成war包并部署到Tomcat中进行测试

在浏览器中输入http://localhost:8080/JerseyFirst/restful/helloworld


测试成功!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值