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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<display-name>RESTfulWS</display-name>
<servlet>
<servlet-name>Way REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.tsh.rest.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Way REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
(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
摘自:http://blog.csdn.net/kkkloveyou/article/details/21391033
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: c/c++ 动态申请数组
- 下一篇: PHP中DES加密解密实例代码