restful完整實例

轉:http://blog.csdn.net/pzasdq/article/details/52566973


口水篇

REST是設計風格而不是標準

  • 資源是由URI來指定。

  • 對資源的操作包括獲取、創建、修改和刪除資源

    這些操作正好對應HTTP協議提供的GET、POST、PUT和DELETE方法。

  • 通過操作資源的表現形式來操作資源。

常用操作

GET獲取
POST提交
PUT更新
Delete刪除

REST確實不是標準,只是設計風格,目的只是讓url看起來更簡潔實用,是資源狀態的一種表達。

實戰篇

Demo下載地址http://pan.baidu.com/s/1o6sJLZs

Maven依賴


<!-- Jar版本管理 -->
<properties>
    <springframework>4.0.2.RELEASE</springframework>
    <log4j>1.2.17</log4j>
    <jstl>1.2</jstl>
</properties>
<dependencies>
    <!-- Spring web mvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework}</version>
    </dependency>
    <!-- JSTL -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl}</version>
    </dependency>
    <!-- log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j}</version>
    </dependency>
    <!-- 單元測試 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

web.xml配置

需要注意,HiddenHttpMethodFilter是針對瀏覽器表單不支持put和delete方法而設計的,通過在表單中設置隱藏域,來分發到相應的處理器上,如<input type="hidden" name="_method" value="put" />

開發後記:最近把RESTful風格融入到了項目中去了,在開發過程中發現一個問題,就是aJax提交的PUT請求,無法通過HiddenHttpMethodFilter這個過濾器拿到值,後來搜索一番,改用HttpPutFormContentFilter


<?xml version="1.0" encoding="UTF-8"?>
<!-- 通過http://java.sun.com/xml/ns/javaee/獲取最新的schemaLocation -->
<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_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>SpringActivemqServer</display-name>
    <!-- WebAppRootKey -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>example.SpringActivemqServer</param-value>
    </context-param>
    <!-- Log4J Start -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <!-- Spring Log4J config -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- Log4J End -->
    <!-- Spring 編碼過濾器 start -->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Spring 編碼過濾器 End -->
    <!-- Spring Application Context Listener Start -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring Application Context Listener End -->
    <!-- Spring MVC Config Start -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- Filter all resources -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Spring MVC Config End -->
    <!-- 隱藏的HttpMethod方法過濾器,表單提交中需要攜帶一個name=_method的隱藏域,value=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>
        <servlet-name>SpringMVC</servlet-name>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-mvc.xml配置


<?xml version="1.0" encoding="UTF-8"?>  
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.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">  
  <!-- 防止@ResponseBody中文亂碼 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <bean class="org.springframework.http.MediaType">
                                <constructor-arg index="0" value="text" />
                                <constructor-arg index="1" value="plain" />
                                <constructor-arg index="2" value="UTF-8" />
                            </bean>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
      <!-- 啓用MVC註解 -->
    <mvc:annotation-driven />
 
    <!-- 靜態資源文件,不會被Spring MVC攔截 -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
     
    <!-- 指定Sping組件掃描的基本包路徑 -->
    <context:component-scan base-package="org.xdemo.example" >
        <!-- 這裏只掃描Controller,不可重複加載Service -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
     
      <!-- JSP視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean>
</beans>

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     <!-- 配置掃描路徑 -->
     <context:component-scan base-package="org.xdemo.example">
       <!-- 只掃描Service,也可以添加Repostory,但是要把Controller排除在外,Controller由spring-mvc.xml去加載 -->
       <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> -->
       <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> -->
       <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /> -->
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
</beans>

User實體類


package org.xdemo.example.springrestful.entity;
/**
 * @作者 Goofy
 * @郵件 [email protected]
 * @日期 2014-4-2下午1:40:32
 * @描述 用戶實體類
 */
public class User {
    private String userId;
    private String userName;
    public User(){}
    public User(String userId,String userName){
        this.userId=userId;
        this.userName=userName;
    }
    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }
    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }
    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }
    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

UserController


package org.xdemo.example.springrestful.controller;
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xdemo.example.springrestful.entity.User;
/**
 * @作者 Goofy
 * @郵件 [email protected]
 * @日期 2014-4-2下午1:28:07
 */
@Controller
@RequestMapping("/user")
public class UserController {
    public List<User> list=null;
    /**
     * user路徑下默認顯示用戶列表
     * @return
     */
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView index(){
        if(list==null){
            list=getUserList();
        }
        ModelMap model=new ModelMap();
        model.addAttribute("list",list);
        return new ModelAndView("user/index",model);
    }
    /**
     * 跳轉到添加用戶頁面,約定優於配置,默認匹配文件/WEB-INF/views/user/add.jsp
     */
    @RequestMapping("add")
    public void add(){}
    /**
     * 新增保存用戶
     * @param user
     * @return ModelAndView
     */
    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView addUser(User user){
        if(list==null){
            list=getUserList();
        }
        list.add(user);
        ModelMap model=new ModelMap();
        model.addAttribute("list",list);
        return new ModelAndView("user/index",model);
    }
    /**
     * 查看用戶詳細信息
     * @param id
     * @return ModelAndView
     */
    @RequestMapping(method=RequestMethod.GET,value="{id}")
    public ModelAndView viewUser(@PathVariable("id")String id){
        User user=findUserById(id);
        ModelMap model=new ModelMap();
        model.addAttribute("user",user);
        return new ModelAndView("user/view",model);
    }
     
    /**
     * 刪除用戶
     * @param id
     */
    @ResponseBody
    @RequestMapping(method=RequestMethod.DELETE,value="{id}")
    public String deleteUser(@PathVariable("id")String id){
        if(list==null){
            list=getUserList();
        }
        removeUserByUserId(id);
        return "suc";
    }
     
    /**
     * 跳轉到編輯頁面
     * @param id
     * @return ModelAndView
     */
    @RequestMapping("{id}/edit")
    public ModelAndView toEdit(@PathVariable("id")String id){
         
        User user=findUserById(id);
        ModelMap model=new ModelMap();
        model.addAttribute("user",user);
         
        return new ModelAndView("user/edit",model);
    }
     
    /**
     * 更新用戶並跳轉到用戶列表頁面
     * @param user
     * @return ModelAndView
     */
    @RequestMapping(method=RequestMethod.PUT)
    public ModelAndView edit(User user){
        updateUser(user);
        return new ModelAndView("redirect:/user/");
    }
     
/********************下面方法是操作數據的*********************/
    /**
     * 造10個用戶
     * @return List<User>
     */
    private List<User> getUserList(){
        List<User> list=new ArrayList<User>();
        for(int i=0; i<10;i++){
            list.add(new User((i+1)+"","李四"+(i+1)));
        }
        return list;
    }
    /**
     * 刪除用戶
     * @param id
     * @return List<User>
     */
    private List<User> removeUserByUserId(String id){
        if(list==null)return null;
        for(User user:list){
            if(user.getUserId().equals(id)){
                list.remove(user);break;
            }
        }
        return list;
    }
    /**
     * 查找用戶
     * @param id
     * @return User
     */
    private User findUserById(String id){
        User user=null;
        if(list==null)return null;
        for(User _user:list){
            if(_user.getUserId().equals(id)){
                user=_user;break;
            }
        }
        return user;
    }
    /**
     * 更新用戶
     * @param user
     */
    private void updateUser(User user){
        for(User _user:list){
            if(_user.getUserId().equals(user.getUserId())){
                _user.setUserName(user.getUserName());break;
            }
        }
    }
     
     
}

用戶列表頁面index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用戶列表</title>
<script type="text/javascript"
    src="<%=basePath%>resources/jquery-1.11.0.min.js"></script>
<style type="text/css">
a {
    border: 1px solid rgb(735858);
    background-color: rgb(133133133);
    height: 50px;
    line-height: 50px;
    color: white;
    text-decoration: none;
    font-weight: bold;
    padding: 5px;
    margin: 5px;
}
</style>
<script type="text/javascript">
 
function deleteUser(id){
    $.ajax({
        type: 'delete',
        url:'<%=basePath%>user/'+id,
        dataType:'text'
        success:function(data){
            if(data=="suc"){
                alert("刪除成功");
                location.reload();
            }
        },
        error:function(data){
        }
    });
}
 
</script>
</head>
 
<body>
    <div style="margin:0 auto;width:500px;">
        <a href="<%=basePath%>user/add">新增用戶</a>
        <table>
            <tr>
                <th>用戶ID</th>
                <th>用戶名稱</th>
                <th>操作</th>
            </tr>
            <c:forEach var="user" items="${list }">
                <tr>
                    <td>${user.userId }</td>
                    <td>${user.userName }</td>
                    <td>
                        <a href="<%=basePath %>user/${user.userId}/edit">編輯用戶</a>
                        <a href="<%=basePath %>user/${user.userId}">查看用戶</a>
                        <a href="javascript:void(0);" onclick="deleteUser(${user.userId })">刪除該用戶</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

編輯用戶頁面edit.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新增用戶頁面</title>
</head>
<body>
    <div style="margin:0 auto;width:400px;">
        <form action="<%=basePath%>user" method="post">
        <!--此隱藏域可以被HiddenHttpMethodFilter所處理,然後分發到不同的HttpMethod的處理器上-->
        <input type="hidden" name="_method" value="put" />
            <table>
                <tr>
                    <th>用戶ID</th>
                    <th>用戶名稱</th>
                </tr>
                <tr>
                    <td><input type="text" name="userId" id="userId" value="${user.userId }" readonly="readonly"/>
                    </td>
                    <td><input type="text" name="userName" id="userName" value="${user.userName }"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="保存用戶" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

新增用戶頁面add.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新增用戶頁面</title>
<meta http-equiv="description" content="This is my page">
</head>
<body>
    <div style="margin:0 auto;width:400px;">
        <form action="<%=basePath%>user" method="post">
            <table>
                <tr>
                    <th>用戶ID</th>
                    <th>用戶名稱</th>
                </tr>
                <tr>
                    <td><input type="text" name="userId" id="userId" />
                    </td>
                    <td><input type="text" name="userName" id="userName" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="保存用戶" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

查看用戶頁面view.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用戶詳情頁面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
    <div style="margin:0 auto;width:400px;">
        <form action="<%=basePath%>user" method="post">
            <table>
                <tr>
                    <th>用戶ID</th>
                    <th>用戶名稱</th>
                </tr>
                <tr>
                    <td>${user.userId}</td>
                    <td>${user.userName}</td>
                </tr>
                <tr>
                    <td colspan="2"><input type="button" value="返回用戶列表"
                        onclick="history.go(-1)" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章