美文网首页
springboot 七-拦截器

springboot 七-拦截器

作者: 小眼睛的露鹿酱 | 来源:发表于2022-02-17 22:57 被阅读0次
    拦截器

    1、 鼻祖 HandlerInterceptor

    1. PreHandle() 执行controller方法之前
    1. postHandle()执行controller方法之后 DispatCherSevlet视图渲染之前 所以这个方法中有一个modelAndView参数 可以做一些修改
    1. afterCompletion() 请求处理完成之后(包括DispatCherSevlet视图渲染之后) 有要求:preHandle()被成功执行且返回true

    代码实战

    1. 创建一个拦截器的类

    package com.example.questionlist2.common;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component
    @Slf4j
    //继承HandlerInterceptor  重新其中的拦截处理防范
    
    public class myInterceptHandle implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            log.info(">>>拦截器>>>>>"+request.getRequestURI());
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        }
    }
    

    2. 对接口配置拦截

    图片.png
    package com.example.questionlist2.common;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component
    @Slf4j
    //继承HandlerInterceptor  重新其中的拦截处理防范
    public class myInterceptHandle implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            log.info(">>>>>>>>"+request.getRequestURI());
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    //        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
       log.info(">>> postHandle  <<<");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    //        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
            log.info(">>> afterCompletion  <<<");
    
        }
    }
    
    postman log

    发现报错:
    cannot deserialize from Object value (no delegate- or property-based Creator) at [
    这个就是需要对error信息中提到的类 创建一个无参构造器

        public questionDto(){
            
        }
    

    正确的拦截

    1. 请求如下

    curl --location --request POST 'http://localhost:8888/addQuestion3' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "questionType":"questionType1",
        "keyword":"keyword1",
        "questionName":"dedede  ",
        "dependOnQuestion":"dependOnQuestion2",
        "book":"book1"
    
    } '
    

    2. 然后结果是包含??? 说明响应body有问题

    有乱码的结构

    3. 对响应结果拦截并处理

    这时候去父类中看下 哪个方法可以对响应结果进行配置

    package com.example.questionlist2.common;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    import java.nio.charset.Charset;
    import java.util.List;
    
    @Configuration
    class myWebMvcConfig extends WebMvcConfigurationSupport {
        @Autowired
        private  myInterceptHandle myInterceptHandle;
        @Override
        //添加拦截器  先把我们创建的拦截器类加进来
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptHandle)
                    .addPathPatterns("/**");
    //                .excludePathPatterns("/addQuestion3");
        }
    
        @Override
        //这时候去父类中看下  哪个方法可以对响应结果进行配置
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            converters.add(setHttpMessageConverter());
        }
      public HttpMessageConverter<String> setHttpMessageConverter(){
            return new StringHttpMessageConverter(Charset.forName("utf-8"));
      }
    
    
    }
    
    图片.png

    相关文章

      网友评论

          本文标题:springboot 七-拦截器

          本文链接:https://www.haomeiwen.com/subject/nxkelrtx.html