什么是RESTful?
RESTful也称为REST(Repressentional State Transfer),可以理解为一种软件架构风格或设计风格,而不是一个标准。
简单来时,RESTful风格就是把请求参数变成请求路径的一种风格。
传统URL请求格式:
http://.../queryItems?id=1
采用RESTful风格之后,其URL请求为:
http://.../items/1
从上述两个请求中可以看出,RESTful风格中的URL将请求参数id=1变成了请求路径中的一部分,并且URL中的queryItems也变成了items(RESTful风格中的URL不存在动词形式的路径,如queryItems表示订单,是一个动词,而items表示订单,为名词)。RESTful风格在HTTP请求中,使用put、delete、post和get方式分别对应添加、删除、修改、查询的操作。
基于RESTful风格的案例
创建User实体类
package com.lwz.po;
/**
* 用户POJO
* 用于封装User类型的请求参数
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
编写springmvc-config.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 定义组件扫描器,指定需要扫描的包 -->
<context:component-scan base-package="com.lwz.controller" />
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources location="/js/" mapping="/js/**" />
<!-- <mvc:default-servlet-handler /> -->
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
创建UserController类
package com.lwz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.itheima.po.User;
@Controller
public class UserController {
/**
*接收RESTful风格的请求,其接收方式为GET
*/
// @RequestMapping用于匹配请求路径和方式
// value="/user/{id}"表示可以匹配以"/user/{id}"结尾的请求
// method=RequestMethod.GET表示只接收GET请求方式的请求
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
// @ResponseBody:用于将Controller的方法返回的对象通过适当的转换器转换为指定的格式,
// 写入到response对象的body区,通常用来返回JSON格式或者是XML
@ResponseBody
// @PathVariable用于接收并绑定请求参数,它可以将请求URL中的变量映射到方法的形参上
public User selectUser(@PathVariable("id") String id){
//查看数据接收
System.out.println("id="+id);
User user=new User();
//模拟根据id查询出到用户对象数据
if(id.equals("1234")){
user.setUsername("tom");
}
//返回JSON格式的数据
return user;
}
}
编写restful.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>
<title>RESTful测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function search(){
// 获取输入的查询编号
var id = $("#number").val();
$.ajax({
url : "${pageContext.request.contextPath }/user/"+id,
type : "GET",
//定义回调响应的数据格式为JSON字符串,该属性可以省略
dataType : "json",
//成功响应的结果
success : function(data){
if(data.username != null){
alert("您查询的用户是:"+data.username);
}else{
alert("没有找到id为:"+id+"的用户!");
}
}
});
}
</script>
</head>
<body>
<form>
编号:<input type="text" name="number" id="number">
<input type="button" value="搜索" onclick="search()" />
</form>
</body>
</html>