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