Idea基于springBoot的拦截器的简单实现-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Idea基于springBoot的拦截器的简单实现

一、创建SpringBoot项目
二、配置文件:
1、启动文件InterceptortestApplication.java

成都创新互联公司是专业的曲沃网站建设公司,曲沃接单;提供网站设计制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行曲沃网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.jane
    interceptortest
    0.0.1-SNAPSHOT
    interceptortest
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


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的拦截器的简单实现


当前题目:Idea基于springBoot的拦截器的简单实现
标题来源:http://kswsj.cn/article/ppgjsi.html

其他资讯