标签:
rest是一个架构风格,用url来访问网络上的任何资源。rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查。
这里介绍的是springMVC的rest请求。 不包含webservice的JAX-RS的例子。rest风格的webservice可以用cxf框架进行实现。也很简单。
1 首先准备web项目需要的jar包,也就是springMVC所依赖的jar:
2 创建一个动态的web工程:这里首先需要配置web.xml文件注册springMVC的前端控制器,dispatcherServlet,所有的客户端请求会被他进行转发。然后在配置hiddenHttpMethodFilter用来把post请求转成put 和 delete
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- hiddenHttpMethodFilter 可以把把post请求转船成put delete --> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springmvc的dispatcherServlet分发请求,实际上他是一个前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3 编写rest风格的接口
package cn.bean.demo.service; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(value="/restservice") public class RestService { public final static String SUCCEEDD="show"; /** * get请求 * url: http://localhost:8080/springmvc/restservice/testRestGet/12 * @param id * 查询的参数 * @return */ @RequestMapping(value="/testRestGet/{id}",method=RequestMethod.GET) public String testRestGet(@PathVariable("id") Integer id){ System.out.println("rest 风格的GET请求..........id=" +id); return SUCCEEDD; } /** * post新增 * url: http://localhost:8080/springmvc/restservice/testRestPost * @return */ @RequestMapping(value="/testRestPost",method=RequestMethod.POST) public String testRestPost(){ System.out.println("rest 风格的POST请求.......... "); return SUCCEEDD; } /** * PUT 修改操作 * url: http://localhost:8080/springmvc/restservice/testRestPut/put123 * @param name * @return */ @RequestMapping(value="/testRestPut/{name}",method=RequestMethod.PUT) public String testRestPut(@PathVariable("name") String name){ System.out.println("rest 风格的PUT请求..........name="+name); return SUCCEEDD; } /** * DELETE删除操作 * url: http://localhost:8080/springmvc/restservice/testRestDelete/11 * @param id * @return */ @RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id){ System.out.println("rest 风格的DELETE请求..........id="+id); return SUCCEEDD; } }
4 编写接口的响应页面 -对应着接口的return
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>show this is succeedd ? yes </h2> </body> </html>
5 发布和配置rest接口 dispatcherServlet-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 --> <context:component-scan base-package="cn.bean.demo"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
6 测试rest接口: 将项目发布到tomcat7中。 测试工具RESTClient
标签:
原文地址:http://www.cnblogs.com/sunjiqiang/p/5595393.html