91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Idea基于springBoot的攔截器的簡單實現

發布時間:2020-07-14 15:33:03 來源:網絡 閱讀:2308 作者:shayang88 欄目:編程語言

一、創建SpringBoot項目
二、配置文件:
1、啟動文件InterceptortestApplication.java

package com.jane.interceptortest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.jane")
public class InterceptortestApplication {

    public static void main(String[] args) {
        SpringApplication.run(InterceptortestApplication.class, args);
    }

}

2、屬性文件,applicaiton.properties

server.port=6009

3、依賴文件,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jane</groupId>
    <artifactId>interceptortest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>interceptortest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4、控制器,TestController

package com.jane.interceptortest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/job")
public class TestController {

    @RequestMapping("/test")
    public String test() {
        System.out.println("RUL 執行 test");
        return "test";
    }

}

5、攔截器TestInterceptor

package com.jane.interceptortest.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class TestInterceptor implements HandlerInterceptor {

    public TestInterceptor(){}

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String queryString = request.getQueryString();
        String requestURL = request.getRequestURL().toString();
        System.out.println("攔截器1,如入參:" + queryString + "訪問地址," + requestURL);
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("攔截器2");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  {
        System.out.println("攔截器3");
    }

}

6、注冊攔截器

package com.jane.interceptortest.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {

    @Autowired
    private TestInterceptor interceptorConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注冊自定義攔截器,添加攔截路徑和排除攔截路徑
        registry.addInterceptor(interceptorConfig).addPathPatterns("/job/**");
    }

}

三、啟動運行
Idea基于springBoot的攔截器的簡單實現
Idea基于springBoot的攔截器的簡單實現

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

北流市| 武功县| 搜索| 威信县| 肇庆市| 蒲城县| 曲阳县| 鹤壁市| 萝北县| 东兰县| 永春县| 新平| 陇南市| 商河县| 新巴尔虎右旗| 垦利县| 类乌齐县| 杭锦后旗| 牟定县| 泰州市| 吉水县| 大渡口区| 阿拉善右旗| 海原县| 朔州市| 牙克石市| 锦州市| 扎鲁特旗| 金溪县| 广宁县| 宜州市| 白水县| 无为县| 出国| 龙门县| 长子县| 盐津县| 东辽县| 桂林市| 方正县| 扶绥县|