文章目录
1、概念
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
2、功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。
传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get
-
http://localhost:8080/springmvc/add.do?a=1&b=1 查询,GET
-
http://localhost:8080/springmvc/insert.do 新增,POST
-
http://localhost:8080/springmvc/update.do 更新,POST
-
http://localhost:8080/springmvc/add/do?id=1,GET或POST
使用RESTful操作资源 :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
-
http://localhost:8080/springmvc/add/1.do 查询,GET
-
http://localhost:8080/springmvc/insert.do 新增,POST
-
http://localhost:8080/springmvc/update.do更新,PUT
-
http://localhost:8080/springmvc/delete/1.do 删除,DELETE
3、学习测试
1、新建一个module项目
2、配置web.xml文件
<?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>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、编写spring-mvc.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: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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="com.jl.controller"/>
<!-- 开启mvc的注解驱动-->
<mvc:annotation-driven/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4、编写RestFulController
package com.jl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class RestFulController {
// @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
@GetMapping("/add/{a}/{b}")
public String hello(@PathVariable int a ,@PathVariable String b , Model model){
String res = a + b;
model.addAttribute("msg","结果是:" + res);
return "test";
}
}
5、在WEB-INF下新建一个views文件存放jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
6、结果展示
7、使用method属性指定请求类型
用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等
我们来测试一下:
- 换一个方法
@Controller
public class RestFulController {
//此处映射访问路径,设置为POST请求
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.POST)
public String hello(@PathVariable int a ,@PathVariable String b , Model model){
String res = a + b;
model.addAttribute("msg","结果是:" + res);
return "test";
}
}
- 我们使用浏览器地址栏进行访问默认是Get请求,会报错405:
- 如果将POST修改为GET则正常了
@Controller
public class RestFulController {
//此处映射访问路径,设置为GET请求
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
public String hello(@PathVariable int a ,@PathVariable String b , Model model){
String res = a + b;
model.addAttribute("msg","结果是:" + res);
return "test";
}
}
小结:
Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的地址栏请求默认都会是 HTTP GET 类型的。
方法级别的注解变体有如下几个:组合注解
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@GetMapping 是一个组合注解,平时使用的会比较多!
它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。