RestFul风格
1、什么是RestFul风格?
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
2、RestFul功能
资源:互联网中所有的东西都可以被抽象定义为资源。
操作资源 :使用 POST、DELETE、PUT、GET方法对资源进行不同的操作。
使用传统的方式对资源进行操作: 不同的参数对应的操作不同。
http://127.0.0.1/item/queryItem.action?id=1 查询,GET
http://127.0.0.1/item/saveItem.action 新增,POST
http://127.0.0.1/item/updateItem.action 更新,POST
http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST
使用RestFul风格方式对资源进行操作: 同一个请求地址可能对应的功能就不同。
http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
3、测试
搭建环境
- 在
web.xml
中注册 DisPatchServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--拦截所有的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 创建对应的Spring配置文件
springmvc-servlet.xml
,配置自动注入HandlerMapping、HandlerAdapter
,注入视图解析器。
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.zyh.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/test" class="com.zyh.controller.HelloController"/>
</beans>
- 编写要请求的JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
- 编写
RestFulController类
package com.zyh.controller;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
//映射的访问路径
@RequestMapping("/test2")
public String test(@Nullable Model model) {
//给视图中传递数据
model.addAttribute("msg","TestController2");
return "test";
}
}
开启服务器测试:
这是使用传统的方式进行访问,参数传递如下图
使用RestFul风格进行传参
- 要在Controller中定义访问的路径映射:
@PathVariable 注解
,让方法参数的值对应绑定到一个URI模板变量上。
@Controller
public class RestFulController {
@RequestMapping("/hello/{a}/{b}")
public String test1(@PathVariable int a, @PathVariable String b, Model model) {
String result = a + b;
model.addAttribute("msg","test1结果="+result);
return "test";
}
}
-
测试
-
使用method属性指定请求类型
用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等
基于方法的组合注解: 就相当于@RequestMapping( method = RequestMethod.GET)
@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
增加一个方法:
@GetMapping("/hello/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable String b, Model model) {
String result = a + b;
model.addAttribute("msg","test1结果="+result);
return "test";
}
测试: