Restful API 简单示例--HelloWorld

Restful API 简单示例--HelloWorld


1. 环境
(1). j2ee luna
(2). Tomcat7
(3). Jersey2.24 (jaxrs-ri-2.24.zip)


2. 流程
(1). 创建DynamicWebProj, eg: RestfulWs_01
(2). 解压jaxrs-ri-2.24.zip,将api/ext/lib文件价下的jar拷贝到WEB-INF/lib下
(3). 新建包com.tsh.rest.resources
(4). 创建类


package com.waylau.rest.resources;


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;


@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello world from tsh";
}

@GET
@Path("/{param}")
@Produces("text/plain;charset=UTF-8") 
public String sayHelloToUTF8(@PathParam("param") String username) {
return "tHello " + username;
}

}


(5) 修改web.xml
 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee" id="WebApp_ID" version="2.5"> 
RESTfulWS 
   
    Way REST Service  
 org.glassfish.jersey.servlet.ServletContainer  
         
    jersey.config.server.provider.packages  
        com.tsh.rest.resources  
       
 
   1  
 
 
   
   
   Way REST Service  
   /rest/*  
 
 



(6). 部署至tomcat,并运行tomcat
(7). 浏览器访问:
http://localhost:8080/RestfulWs_01/rest/hello
http://localhost:8080/RestfulWs_01/rest/hello/1
http://localhost:8080/RestfulWs_01/rest/hello/2
http://localhost:8080/RestfulWs_01/rest/hello/5

Restful API 简单示例--HelloWorld_第1张图片 Restful API 简单示例--HelloWorld_第2张图片 Restful API 简单示例--HelloWorld_第3张图片

摘自:http://blog.csdn.net/kkkloveyou/article/details/21391033



你可能感兴趣的:(Restful API 简单示例--HelloWorld)