SpringMVC的REST风格的四种请求方式

知识点:

  1. 什么是rest风格
  2. 实例
  3. 总结

一.rest 风格

  • REST : 即 Representational State Transfer 。(资源)表现层状态转化。
  • 资源 : 网络上的一个实体或者说是网络上的一个具体信息。 每种资源对应一个特定的URI,因此URI为每一个资源的独一无二的识别符。
  • 状态转化 : 每发出一个请求,就代表了客户端和服务器端的一次交互过程。HTTP协议是一个无状态协议,即所有的状态都保存在服务器上。

因此用户想要操作服务器,必须通过某种手段,让服务器发生状态变化。而这种状态变化是建立在表现层之上的,所以就是“表现层状态变化”。

具体的说,就是HTTP协议里面四个表示操作的方法:

  1. GET : 用来获取资源 ;
  2. POST : 用来新建资源 ;
  3. PUT : 用来更新资源 ;
  4. DELETE : 用来删除资源 。
     

二.实例:使用注解开发spring mvc

流程:

  1. 导包
  2. 配置web.xml:配置一个Filter来支持rest风格
  3. 创建springmvc-servlet.xml:使用注解的方式开发Spring mvc
  4. 创建Controller.java
  5. 创建index.jsp
  6. 创建hello.jsp

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" xmlns:web="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_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<!--配置HiddenHttpMethodFilter此filter支持rest风格,过滤器可以将POST请求转化为put请求和delete请求!-->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Spingmvc-servlet.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
    <!-- 第一步:注解扫描 -->
    <context:component-scan base-package="controller"/>	

    <!-- 第二步 -->
    <!-- 配置sprigmvc视图解析器:解析逻辑试图(配置jsp 显示ViewResolver)
             后台返回逻辑试图:index
            视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/page/*.jsp-->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"	value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
   
</beans>

Controller.java

package controller;

//测试第二种方法
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class IndexController{
   /*@ResponseBody是用来将itemsCustom转成json的注解,而@PathVariable注解则是跟REST有关了,
        @RequestMapping(value=”/itemsView/{id}”)中的{id}表示一个占位符,
        那么这里面传过来的值会传到被@PathVariable标记的形参上,
        如果形参和占位符中的变量一样的话,可以不用再注解中指定,
        否则注解中要指定这个占位符中的变量(即id)。这样的话,参数就可以通过url传到形参中来了。*/

	//GET
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	public String testGet(@PathVariable("id")Integer id){
		System.out.println("GET:"+id);
		return "hello";
	}
    
        //POST
	@RequestMapping(value="/user/{id}",method=RequestMethod.POST)
	public String testPost(@PathVariable("id")Integer id){
		System.out.println("POST:"+id);
		return "hello";
	}

        //PUT
	@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
	public String testPut(@PathVariable("id")Integer id){
		System.out.println("PUT:"+id);
		return "hello";
	}

        //DELETE
	@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
	public String testDelete(@PathVariable("id")Integer id){
		System.out.println("DELETE:"+id);
		return "hello";
	}
}

index.jsp

  • 关于PUT和DELETE,均是在表单中设置隐藏域,name='_method',value设置为对应值。然后form表单发送POST请求,由HiddenHttpMethodFilter根据_method的值将POST请求转换为PUT请求或者DELETE请求。
<%@ 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>
</head>
<body>
        <!--get-->
	<a href="user/123">Get请求</a>
	
        <!--post-->
	<form action="user/123" method="post">
		<!--<input type="hidden" name="_method" value="POST">-->
		<input type="submit" value="post请求">
	</form>
	
        <!--put-->
	<form action="user/123" method="post">
		<input type="hidden" name="_method" value="PUT">
		<input type="submit" value="put请求">
	</form>
	
        <!--delete-->
	<form action="user/123" method="post">
		<input type="hidden" name="_method" value="DELETE">
		<input type="submit" value="delete请求">
	</form>
	
</body>
</html>

hello.java

<%@ 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>
</head>
<body>
	Hello SpringMVC
	<h1>${message}</h1>
</body>
</html>

控制台输出:

GET:123
POST:123
PUT:123
DELETE:123

三.总结

以往都是不同的http请求,url不同,使用rest风格之后,url相同,请求方式不同。

rest风格简单来说在同一个路径下,不同的协议请求(Get、Post、Put、Delete、Patch、Head、Option s、Trace)实现不同的功能。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值