当前位置:网站首页>Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
2022-07-27 04:27:00 【Time is an antidote】
Use WebMvcConfigurer Interface request interception in progress ( Source code attached )
The problem background
Interceptors are often used in projects to intercept requests, print logs, input and output , Introduce to you WebMvcConfigurer Use of interceptors
matters needing attention :
- You can use the code of the article to create your own project , You can also download the source code for reference
WebMvcConfigurer The interceptor is enhanced
1 Add the configuration class of interceptor
package com.lanran.webmvcconfigurer.config;
import com.lanran.webmvcconfigurer.interceptor.WebInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * @Description: * @Created: IDEA2021 * @author: Blue dye * @createTime: 2022-07-30 17:57 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebInterceptor()) // Register interceptors ,
.addPathPatterns("/**"); // All requests need to be intercepted
}
}
2 Add interceptor
package com.lanran.webmvcconfigurer.interceptor;
import com.lanran.webmvcconfigurer.entity.UserInfoTo;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** * @Description: Before executing the target method , Judge the user's login status . Is a temporary user , Or login user , And encapsulate and pass it to controller Target request * @Created: IDEA2021 * @author: Blue dye * @createTime: 2022-07-30 17:31 **/
// The interceptor needs to be configured to take effect , Add configuration class GulimallWebConfig implements WebMvcConfigurer
//HandlerInterceptor This is MVC Interceptor , So all of them getmapping and postmapping Will be intercepted
public class WebInterceptor implements HandlerInterceptor {
/*** * Before the target method is executed , The target method here * @param request * @param response * @param handler * @return * @throws Exception */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("before");
return true;
}
/** * After business execution , Assign temporary users to the browser to save * * @param request * @param response * @param handler * @param modelAndView * @throws Exception */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("after");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3 Add test interface
package com.lanran.webmvcconfigurer.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/** * @Author suolong * @Date 2022/7/22 9:52 * @Version 2.0 */
@RestController
public class WebController {
@GetMapping("/test")
public String test(){
System.out.println("test method");
return "success";
}
}
4 Entity classes used
package com.lanran.webmvcconfigurer.entity;
import lombok.Data;
/** * @Author suolong * @Date 2022/7/22 8:44 * @Version 2.0 */
@Data
public class UserInfoTo {
private String name;
private String gender;
private Integer age;
private Boolean flag;
}
5 Start class
package com.lanran.webmvcconfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebMvcConfigurerApplication {
public static void main(String[] args) {
SpringApplication.run(WebMvcConfigurerApplication.class, args);
}
}
6 Project directory 
7 Startup items , Use postman Test interface 
Log print results 
summary
Stop what you want to stop
As a programmer 214 An article , Write one line of lyrics at a time and record , Look at how many songs there are in life ,wahahaha …


Lyric: In the rain with tears
边栏推荐
- Learning route from junior programmer to architect + complete version of supporting learning resources
- Rust:axum学习笔记(1) hello world
- [C language] recursively explain the tower of Hanoi problem
- Principle of bean validation --07
- Preliminary understanding of NiO
- DINO 论文精度,并解析其模型结构 & DETR 的变体
- 项目参数做成可配置项,@ConfigurationProperties注解的使用
- sram、dram、sdram、ddr的区别和用途
- centos如何安装mysqldump
- Delete the whole line of Excel, and delete the pictures together
猜你喜欢

Is VR panoramic production a single weapon in the home decoration industry? Why is this?

Okaleido ecological core equity Oka, all in fusion mining mode

【小样本分割】MSANet: Multi-Similarity and Attention Guidance for Boosting Few-Shot Segmentation

Introduction to JVM principle

卷积神经网络——灰度图像的卷积

Plato Farm全新玩法,套利ePLATO稳获超高收益

通信协议综述

你了解微信商户分账吗?

Nacos startup and login

Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
随机推荐
Plato Farm全新玩法,套利ePLATO稳获超高收益
一张图看懂KingbaseES V9
Internet of things smart home project - Smart bedroom
JS modify the key value of the object array
Leetcode daily exercise: sort sentences
对NIO的初步理解
Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
Remember the major performance problems caused by a TCP packet loss
Session&Cookie&token
Framework learning journey: init process startup process
[leetcode] day104 no overlapping interval
tcp协议知识详解
[MySQL series] MySQL index transactions
【无标题】
[C language] recursively explain the tower of Hanoi problem
php+swoole
Learning route from junior programmer to architect + complete version of supporting learning resources
Brightcove任命Dan Freund为首席营收官
Nacos startup and login
xxx is not in the sudoers file.This incident will be reported.的解决方法