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

溫馨提示×

溫馨提示×

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

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

SpringBoot添加自定義攔截器的實現代碼

發布時間:2020-09-29 05:25:31 來源:腳本之家 閱讀:186 作者:紫薇帝星的故事 欄目:編程語言

在Controller層時,往往會需要校驗或驗證某些操作,而在每個Controller寫重復代碼,工作量比較大,這里在Springboot項目中 ,通過繼承WebMvcConfigurerAdapter,添加攔截器。

1、WebMvcConfigurerAdapter源碼

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.web.servlet.config.annotation;
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
/**
 * An implementation of {@link WebMvcConfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 *
 * @author Rossen Stoyanchev
 * @since 3.1
 */
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addFormatters(FormatterRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation is empty.
   */
  @Override
  public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  }
  /**
   * {@inheritDoc}
   * <p>This implementation returns {@code null}.
   */
  @Override
  public Validator getValidator() {
    return null;
  }
  /**
   * {@inheritDoc}
   * <p>This implementation returns {@code null}.
   */
  @Override
  public MessageCodesResolver getMessageCodesResolver() {
    return null;
  }
}

可以看出,該類 還能配置其他很多操作,例如異常處理,跨域請求等配置。

2、自動義Web配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
  }
  @Bean
  public MyInterceptor getMyInterceptor(){
    return new MyInterceptor();
  }
}

  如果需要添加多個攔截器,InterceptorRegistry registry.addInterceptor方法

public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
    InterceptorRegistration registration = new InterceptorRegistration(interceptor);
    this.registrations.add(registration);
    return registration;
  }

registrations是個數組結構,可以添加多個

3、自動義攔截器

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class MyInterceptor extends HandlerInterceptorAdapter {
  final Logger logger = LoggerFactory.getLogger(getClass());
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //攔截操作
    return true;
  }
}

總結

以上所述是小編給大家介紹的SpringBoot添加自定義攔截器的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

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

AI

明星| 长岭县| 靖州| 台山市| 涪陵区| 贞丰县| 墨脱县| 许昌市| 常山县| 南投县| 连平县| 四子王旗| 大足县| 大同县| 县级市| 呼和浩特市| 府谷县| 安达市| 富川| 体育| 斗六市| 香河县| 徐水县| 江都市| 平阳县| 北安市| 黑山县| 壶关县| 临泽县| 大渡口区| 博白县| 新建县| 南汇区| 收藏| 英德市| 驻马店市| 南城县| 津南区| 陆河县| 建昌县| 汝城县|