首页 > 代码库 > 【Spring】SpringMVC之REST编程风格

【Spring】SpringMVC之REST编程风格

REST架构是一个抽象的概念,目前主要是基于HTTP协议实现,其目的是为了提高系统的可伸缩性、降低应用之间的耦合度、便于架构分布式处理程序。

 

在URL中设置使用如下方式: /{变量名1}/{变量名2}

在代码中向Controller方法注入参数:  (@PathVariable("变量名1") String str1,@PathVariable("变量名2") String str2) 
例如:

        @RequestMapping(value="http://www.mamicode.com/book/{bookid}",method=RequestMethod.POST)        @ResponseBody        public Object getBook(@PathVariable("bookid") String bookid){            return new Book();        }

 

下面是一个使用案例:

技术分享

bean类:

技术分享
package cn.xdl.bean;public class Book {    private int bid;    private String bname;    public int getBid() {        return bid;    }    public void setBid(int bid) {        this.bid = bid;    }    public String getBname() {        return bname;    }    public void setBname(String bname) {        this.bname = bname;    }    public Book(int bid, String bname) {        super();        this.bid = bid;        this.bname = bname;    }    public Book() {        super();        // TODO Auto-generated constructor stub    }    @Override    public String toString() {        return "Book [bid=" + bid + ", bname=" + bname + "]";    }    }
Book.java

Controller类

技术分享BookController.java

这里的通信方式返回的结果是Object对象。

bean.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:jdbc="http://www.springframework.org/schema/jdbc"      xmlns:jee="http://www.springframework.org/schema/jee"     xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">        <!--             开启注解扫描         -->         <context:component-scan base-package="cn"></context:component-scan>         <!--              开启mvc注解扫描          -->        <mvc:annotation-driven/>                <mvc:default-servlet-handler/>                </beans>
bean.xml

在配置文件中一定需要指出:

<mvc:default-servlet-handler/>

否则会出现静态资源(如js、css等资源)拦截404的错误。

web.xml文件

技术分享
<?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" version="3.0">    <servlet>        <servlet-name>webmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:bean.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>webmvc</servlet-name>        <url-pattern>/</url-pattern>            </servlet-mapping></web-app>
web.xml

web.xml文件过滤的url必须写成 <url-pattern>/</url-pattern> ,因为采用REST编程风格,在url地址中只会出现/分割符号,所以只能写成这样 <url-pattern>/</url-pattern> ,以匹配所有的路径。

restTest.jsp文件

技术分享
<%@ 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><script type="text/javascript" src="http://www.mamicode.com/js/jquery.js"></script><script type="text/javascript">    function insertClick(){        $.ajax({            url:"book/10003/hahaha",            dataType:"JSON",            type:"POST",            success:function(data){                alert( JSON.stringify(data));            },            error:function(){                            }        });            }    function deleteClick(){        $.ajax({            url:"book/10003",            dataType:"JSON",            type:"DELETE",            success:function(data){                alert( JSON.stringify(data));            },            error:function(){                            }        });                    }    function updateClick(){        $.ajax({            url:"book/10003/hahaha",            dataType:"JSON",            type:"PUT",            success:function(data){                alert( JSON.stringify(data));            },            error:function(){                            }        });            }    function findClick(){        $.ajax({            url:"book/10003",            dataType:"JSON",            type:"GET",            success:function(data){                alert( JSON.stringify(data));            },            error:function(){                            }        });            }</script></head><body>    <input onclick="insertClick()" type="button" value="http://www.mamicode.com/增加"/><br><br>    <input onclick="deleteClick()" type="button" value="http://www.mamicode.com/删除"/><br><br>    <input onclick="updateClick()" type="button" value="http://www.mamicode.com/修改"/><br><br>    <input onclick="findClick()" type="button" value="http://www.mamicode.com/查询"/><br><br></body></html>
restTest.jsp

这里通信方式是采用Ajax请求。

【Spring】SpringMVC之REST编程风格