zoukankan      html  css  js  c++  java
  • 用Jersey构建RESTful服务1--HelloWorld

    一、环境
    1、Eclipse Juno R2
    2. Tomcat 7
    3. Jersey 2.7  下载地址( https://jersey.java.net/download.html)


    二、流程
    1.Eclipse 中创建一个 Dynamic Web Project ,本例为“RestDemo”

    2.按个各人习惯建好包,本例为“com.waylau.rest.resources”

    3.解压jaxrs-ri-2.7,

    将api、ext、lib文件夹下的jar包都放到项目的lib下;

    项目引入jar包

    4.在resources包下建一个class“HelloResource”

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.waylau.rest.resources;  
    2.   
    3.   
    4. import javax.ws.rs.GET;  
    5. import javax.ws.rs.Path;  
    6. import javax.ws.rs.Produces;  
    7. import javax.ws.rs.PathParam;  
    8. import javax.ws.rs.core.MediaType;  
    9.   
    10.   
    11. @Path("/hello")  
    12. public class HelloResource {  
    13.     @GET  
    14.     @Produces(MediaType.TEXT_PLAIN)  
    15.     public String sayHello() {  
    16.         return "Hello World!" ;  
    17.     }  
    18.    
    19.       
    20.     @GET  
    21.     @Path("/{param}")    
    22.     @Produces("text/plain;charset=UTF-8")  
    23.     public String sayHelloToUTF8(@PathParam("param") String username) {  
    24.         return "Hello " + username;  
    25.     }  
    26.       
    27. }  





    5.修改web.xml,添加基于Servlet-的部署

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <servlet>    
    2.     <servlet-name>Way REST Service</servlet-name>  
    3.  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    4.       <init-param>    
    5.     <param-name>jersey.config.server.provider.packages</param-name>  
    6.         <param-value>com.waylau.rest.resources</param-value>  
    7.        </init-param>  
    8.    <load-on-startup>1</load-on-startup>  
    9.  </servlet>  
    10.    
    11.  <servlet-mapping>  
    12.    <servlet-name>Way REST Service</servlet-name>  
    13.    <url-pattern>/rest/*</url-pattern>  
    14.  </servlet-mapping>  





    6.项目部署到tomcat,运行
    7.浏览器输入要访问的uri地址
    http://localhost:8089/RestDemo/rest/hello

    输出Hello World!



    http://localhost:8089/RestDemo/rest/hello/Way你好吗


    输出Hello Way你好吗

     

    参考:https://jersey.java.net/documentation/latest/user-guide.html

  • 相关阅读:
    9月7日总结
    Arbitrage题解
    杀蚂蚁题解
    8月11日总结
    8月10总结
    PHP 关于获取客户端ip的方法
    PHP内置函数大全
    PHP header函数设置http头
    获取两个日期之间的全部的日期数据(包括两个日期)
    根据周日获取这周的周日到周六的日期(周日为这周的第一天)
  • 原文地址:https://www.cnblogs.com/langtianya/p/6870319.html
Copyright © 2011-2022 走看看