IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Spring MVC 教程(三):REST风格

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

一、什么叫REST

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用

• 资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。

• 表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。

• 状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。

二、看完上面对REST的解释是不是一脸懵比呢,没关系我们通过代码实例来解读

实际上REST就是一种新型的CRUD操作方式,那么SpringMVC是怎么实现的呢

1、 需要在web.xml配置 HiddenHttpMethodFilter

 <?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_2_5.xsd"
     id="WebApp_ID" version="2.5">

    <!-- 
     配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 put 请求 
     -->
     <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>

     <!-- 配置 DispatcherServlet -->
     <servlet>
         <servlet-name>dispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
         <!-- 
             实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
             默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
         -->
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:springmvc.xml</param-value>
         </init-param>
         <!-- 加载时创建 -->
         <load-on-startup>1</load-on-startup>
     </servlet>

     <!-- dispatcherServlet可以应答所有请求 -->
     <servlet-mapping>
         <servlet-name>dispatcherServlet</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>

 </web-app>

HiddenHttpMethodFilter源码:

 @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
             throws ServletException, IOException {

         HttpServletRequest requestToUse = request;

         if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
             String paramValue = request.getParameter(this.methodParam);
             if (StringUtils.hasLength(paramValue)) {
                 requestToUse = new HttpMethodRequestWrapper(request, paramValue);
             }
         }

         filterChain.doFilter(requestToUse, response);
     }

通过源码可以知道SpringMVC可以把post请求转换为delete和put,我们只需要在发送 POST 请求时携带一个 name=”_method” 的隐藏域, 值为 DELETE 或 PUT即可

2、在index.jsp编写REST风格的CRUD请求

 <!--Rest风格 begin  --> 
 <br/>
 <a href="restTest/testRest/1">Test Rest Get</a>

 <br/>
 <form action="restTest/testRest" method="post">
         <input type="submit" value="TestRest POST"/>
 </form>

 <br/>
 <form action="restTest/testRest/1" method="post">
     <input type="hidden" name="_method" value="DELETE"/>
     <input type="submit" value="TestRest DELETE"/>
 </form>

 <br/>
 <form action="restTest/testRest/1" method="post">
         <input type="hidden" name="_method" value="PUT"/>
         <input type="submit" value="TestRest PUT"/>
 </form>
 <!--Rest风格 end  --> 

3、编写handler

 package com.study.springmvc.handlers;

 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;

 @RequestMapping("/restTest")
 @Controller
 public class RestTest {

     public static final String SUCCESS="success";

     /**
      * Rest 风格的 URL. 
      * 以 CRUD 为例: 
      * 新增: /order POST 
      * 修改: /order/1 PUT update?id=1 
      * 获取: /order/1 GET get?id=1 
      * 删除: /order/1 DELETE delete?id=1
      * 
      * 如何发送 PUT 请求和 DELETE 请求呢 ? 
      * 1. 需要在web.xml配置 HiddenHttpMethodFilter 
      * 2. 需要发送 POST 请求
      * 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
      * 
      * 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解
      * 
      */

     /**
      * Rest风格:get请求
      * @param id
      * @return
      */
     @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
     public String testRest(@PathVariable Integer id) {
         System.out.println("testRest GET: " + id);
         return SUCCESS;
     }

     /**
      * Rest风格:post请求
      * @param id
      * @return
      */
     @RequestMapping(value = "/testRest", method = RequestMethod.POST)
     public String testRest() {
         System.out.println("testRest POST");
         return SUCCESS;
     }

     /**
      * Rest风格:delete请求
      * @param id
      * @return
      */
     @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
     public String testRestDelete(@PathVariable Integer id) {
         System.out.println("testRest Delete: " + id);
         return SUCCESS;
     }

     /**
      * Rest风格:put请求(更新)
      * @param id
      * @return
      */
     @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
     public String testRestPut(@PathVariable Integer id) {
         System.out.println("testRest Put: " + id);
         return SUCCESS;
     }

 }

https://www.cnblogs.com/leeSmall/category/1093236.html

文章永久链接:https://tech.souyunku.com/?p=13923


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(98) 打赏



未经允许不得转载:搜云库技术团队 » Spring MVC 教程(三):REST风格

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367