REST架构是一个抽象的概念,目前主要是基于HTTP协议实现,其目的是为了提高系统的可伸缩性、降低应用之间的耦合度、便于架构分布式处理程序。当使用多种语言进行开发的时候,每一种语言对URL的处理不同,这时候就需要统一处理,那么使用REST编程风格就很有必要了。REST只是一种设计模式,如果需要对安全性有要求,就是额外的功能代码了。

使用方式

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

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

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

Demo

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类:

package cn.xdl.c;

import java.util.HashMap;
import java.util.Map; 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;
import org.springframework.web.bind.annotation.ResponseBody; import cn.xdl.bean.Book; @Controller
public class BookController { /**
* VALUE :请求的地址模版 (模版中使用大括号括住的部分, 属于变量)
* method: 请求的方式
* @return
*/
@RequestMapping(value="/book/{bookid}",method=RequestMethod.GET)
@ResponseBody
public Object insertBook(@PathVariable("bookid") String bookId) {
System.out.println("正在查询图书 , 请求的图书的编号为:"+bookId);
return new Book(Integer.parseInt(bookId), "欢乐颂");
}
@RequestMapping(value="book/{bookid}",method=RequestMethod.DELETE)
@ResponseBody
public Object deleteBook(@PathVariable("bookid")String bookid) {
System.out.println("正在删除图书:id:"+bookid);
Map<String,String> map = new HashMap<String,String>();
map.put("errorCode0", "0");
map.put("msg", "删除成功");
return map;
}
@RequestMapping(value="book/{bookid}/{bookname}",method=RequestMethod.PUT)
@ResponseBody
public Object updateBook(@PathVariable("bookid") String bookId,@PathVariable("bookname") String bookName) {
System.out.println("正在修改图书:id:"+bookId+", 要修改的新书名:"+bookName);
Map<String,String> map = new HashMap<String,String>();
map.put("errorCode0", "0");
map.put("msg", "修改成功");
return map;
} @RequestMapping(value="book/{bookid}/{bookname}",method=RequestMethod.POST)
@ResponseBody
public Object insertBook(@PathVariable("bookid") String bookId,@PathVariable("bookname") String bookName) {
System.out.println("正在添加图书:id:"+bookId+", 要添加的新书名:"+bookName);
Map<String,String> map = new HashMap<String,String>();
map.put("errorCode0", "0");
map.put("msg", "添加成功");
return map;
} }

BookController.java

这里的通信方式返回的结果是Object对象。在这个笔者使用了注解 @ResponseBody ,需要导入包 jackson-annotations.jar 、 jackson-core.jar 和 jackson-databind.jar包。

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="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="增加"/><br><br>
<input onclick="deleteClick()" type="button" value="删除"/><br><br>
<input onclick="updateClick()" type="button" value="修改"/><br><br>
<input onclick="findClick()" type="button" value="查询"/><br><br>
</body>
</html>

restTest.jsp

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

Rest请求地址中带有中文的处理

如果在Rest风格的请求中,请求的参数带有中文,就会出现映射错误。这时候需要进行如下的配置:

在SpringMVC的配置文件中将 <mvc:annotation-driven/> 改为:

<!--开始注解-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

除此之外还需要在tomcat 的 server.xml中,加入URIEncoding="UTF-8":

    <Connector connectionTimeout="20000" port="8081" URIEncoding="UTF-8" protocol="HTTP/1.1" redirectPort="8443"/>

这样就可以解决Rest请求中中文乱码的问题了。

【Spring】SpringMVC之REST编程风格的更多相关文章

  1. Spring+SpringMVC+Mybatis大整合(SpringMVC采用REST风格、mybatis采用Mapper代理)

    整体目录结构: 其中包下全部是采用mybatis自动生成工具生成. mybatis自动生成文件 <?xml version="1.0" encoding="UTF- ...

  2. Eclipse Meaven Spring SpringMVC Mybaits整合

    本示例是在:Ubuntu15上实现的:Windows上安装Maven将不太相同. Maven Install Run command sudo apt-get install maven, to in ...

  3. myBatis+Spring+SpringMVC框架面试题整理

    myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM   版权声明:本文为博主原创文章 ...

  4. java web后台开发SSM框架(Spring+SpringMVC+MyBaitis)搭建与优化

    一.ssm框架搭建 1.1创建项目 新建项目后规划好各层的包. 1.2导入包 搭建SSM框架所需包百度云链接:http://pan.baidu.com/s/1cvKjL0 1.3整合spring与my ...

  5. myBatis,Spring,SpringMVC三大框架ssm整合模板

    整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration>    <!-- 批量别名的设置 -- ...

  6. Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建

    1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)--S ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(十四)——SpringMVC和MyBatis整合

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十三)--S ...

随机推荐

  1. Windows远程桌面连接的利器-mRemote

    mRemoteNG是Windows平台下一款开源的支持多标签.多协议的远程连接管理器.平时我们可能安装N多款管理工具,如putty.SecureCRT.xshell.SSHshell.mstsc.ex ...

  2. “Uncaught TypeError: Cannot call method 'createChild' of undefined" 问题的解决

    Uncaught TypeError: Cannot call method 'createChild' of undefined 我在使用Ext 4.1.1做grid.Panel,然后chrome爆 ...

  3. MVC4发布到IIS,出现HTTP 错误 404.0 - Not Found

    web.config中添加 <system.webServer> <modules runAllManagedModulesForAllRequests="true&quo ...

  4. android studio中的常用快捷键

    1.Ctrl+Alt+Space 这个类似Eclipse中的Alt+/,实现智能提示功能的 2.Ctrl+Y 删除当前行,Eclipse中是Ctrl+D,伤不起,每次都习惯性的按Ctrl+D,不删,反 ...

  5. iOS 开发者证书总结 in-house

    iOS 证书分两种类型. 第一种为$99美元的,这种账号有个人和公司的区别,公司账号能创建多个子账号,但个人的不能.这种账号可以用来上传app store 第二种为¥299美元的,这种账号只能用于企业 ...

  6. CheeseZH: Stanford University: Machine Learning Ex5:Regularized Linear Regression and Bias v.s. Variance

    源码:https://github.com/cheesezhe/Coursera-Machine-Learning-Exercise/tree/master/ex5 Introduction: In ...

  7. 批量删除SQL数据库中的所有表【笔记】

    use OTRJiraDB GO ) begin SELECT @sql='drop table ' + name FROM sysobjects WHERE (type = 'U') ORDER B ...

  8. Code Review学习笔记

    一:Code Review的必要性 代码审查,可以帮助他人发现不足,也可以促进自己培养良好的编程习惯. 自我代码审查,可以在编码完成之后,对自己的代码进行整理,发现“味道不好”的代码,作进一步的调整. ...

  9. SSM实战——秒杀系统之Web层Restful url设计、SpringMVC整合、页面设计

    一:Spring整合SpringMVC 1:编写web.xml,配置DispatcherServlet <web-app xmlns="http://java.sun.com/xml/ ...

  10. Shell编程初步

      一:Hello World 新建一个文件,命名时以 .sh 为后缀.每个bash文件开头第一行表名文件类型: #!/bin/bash 然后在下面输入代码. 比如输出hello world: #!/ ...