当前位置: 首页 > news >正文

品牌网站设计公司哪家百度公司简介

品牌网站设计公司哪家,百度公司简介,软件平台制作,学平面设计在哪里学当使用Spring Boot时,我们可以通过拦截器(Interceptor)和监听器(Listener)来实现对请求和响应的处理。拦截器和监听器提供了一种可插拔的机制,用于在请求处理过程中进行自定义操作,例如记录日志…

当使用Spring Boot时,我们可以通过拦截器(Interceptor)和监听器(Listener)来实现对请求和响应的处理。拦截器和监听器提供了一种可插拔的机制,用于在请求处理过程中进行自定义操作,例如记录日志、身份验证、权限检查等。下面通过提供一个示例,展示如何使用拦截器和监听器来记录请求日志。

首先,我们创建一个简单的Spring Boot项目,并添加所需的依赖。在这个示例中,我们将使用Spring Boot Starter Web。

  1. 创建一个Spring Boot项目并添加依赖

创建一个新的Spring Boot项目,可以使用Spring Initializr(https://start.spring.io/)进行初始化。在"Dependencies"中添加"Spring Web"依赖,并生成项目。

  1. 创建拦截器

在项目中创建一个名为 RequestLoggingInterceptor 的类,实现 HandlerInterceptor 接口。这个拦截器将记录请求的URL、HTTP方法和时间戳。

import org.springframework.web.servlet.HandlerInterceptor;

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

public class RequestLoggingInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 记录请求的URL、HTTP方法和时间戳
        System.out.println("RequestLoggingInterceptor"+"启动了");
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("HTTP Method: " + request.getMethod());
        System.out.println("Timestamp: " + System.currentTimeMillis());
        return true;
    }
}
  1. 注册拦截器

在Spring Boot应用程序的配置类中,注册拦截器,使其生效。

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.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final RequestLoggingInterceptor requestLoggingInterceptor;

    @Autowired
    public WebConfig(RequestLoggingInterceptor requestLoggingInterceptor) {
        this.requestLoggingInterceptor = requestLoggingInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry.addInterceptor(requestLoggingInterceptor);
    }
}
  1. 创建监听器

在项目中创建一个名为 RequestListener 的类,实现 ServletRequestListener 接口。这个监听器将在请求的开始和结束时记录日志。

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

@WebListener
public class RequestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        System.out.println("RequestListener"+"启动了");
        // 记录请求的URL、HTTP方法和时间戳
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("HTTP Method: " + request.getMethod());
        System.out.println("Timestamp: " + System.currentTimeMillis());
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // 请求处理完成后的操作
        System.out.println("Request processing completed.");
    }
}
  1. 编写控制器

创建一个简单的控制器来模拟请求处理

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/user")
    public String getUser() {
        return "Get User";
    }

    @PostMapping("/user")
    public String saveUser(@RequestBody String user) {
        return "Save User: " + user;
    }
}

  1. 在启动类或配置类上添加 @ServletComponentScan 注解

启用对监听器的支持

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.classargs);
    }
}

  1. 运行应用程序

现在,你可以运行Spring Boot应用程序并访问一些URL,观察控制台输出的日志信息。每次发起请求时,拦截器和监听器都会捕获请求并输出相关的日志。示例效果如下:

alt

本文由 mdnice 多平台发布


文章转载自:
http://dinncoungovernable.ssfq.cn
http://dinncodysentery.ssfq.cn
http://dinncodianthus.ssfq.cn
http://dinncochon.ssfq.cn
http://dinncoxenophobic.ssfq.cn
http://dinncoprotease.ssfq.cn
http://dinncosixscore.ssfq.cn
http://dinncoepigraph.ssfq.cn
http://dinncofreezes.ssfq.cn
http://dinncoliterarily.ssfq.cn
http://dinncogustatorial.ssfq.cn
http://dinncothermophysics.ssfq.cn
http://dinncogrotesquery.ssfq.cn
http://dinncorenew.ssfq.cn
http://dinncomultination.ssfq.cn
http://dinncogabfest.ssfq.cn
http://dinncomycetoma.ssfq.cn
http://dinncofarl.ssfq.cn
http://dinncosalability.ssfq.cn
http://dinncohepatocele.ssfq.cn
http://dinncobrasilia.ssfq.cn
http://dinncosaccharinated.ssfq.cn
http://dinncoscopey.ssfq.cn
http://dinncoaveragely.ssfq.cn
http://dinncohadrosaur.ssfq.cn
http://dinncotransplantate.ssfq.cn
http://dinncohydromel.ssfq.cn
http://dinncoshortly.ssfq.cn
http://dinncotitubation.ssfq.cn
http://dinncosylvestral.ssfq.cn
http://dinncodebrett.ssfq.cn
http://dinncophleboid.ssfq.cn
http://dinncoalpeen.ssfq.cn
http://dinncoerythropoiesis.ssfq.cn
http://dinncoflotage.ssfq.cn
http://dinncotale.ssfq.cn
http://dinncopygmy.ssfq.cn
http://dinncoladderback.ssfq.cn
http://dinncoconsuelo.ssfq.cn
http://dinncoyokemate.ssfq.cn
http://dinncoderequisition.ssfq.cn
http://dinncoredbud.ssfq.cn
http://dinncopagehood.ssfq.cn
http://dinncokilim.ssfq.cn
http://dinncouvulotomy.ssfq.cn
http://dinncovasodilatation.ssfq.cn
http://dinncocompotation.ssfq.cn
http://dinncolibrettist.ssfq.cn
http://dinncofalernian.ssfq.cn
http://dinncocryptoanalysis.ssfq.cn
http://dinncodevolution.ssfq.cn
http://dinncomojave.ssfq.cn
http://dinncogradienter.ssfq.cn
http://dinncoohio.ssfq.cn
http://dinncolegitimise.ssfq.cn
http://dinncoseparative.ssfq.cn
http://dinncolapland.ssfq.cn
http://dinncoprosthodontia.ssfq.cn
http://dinncoeurythmy.ssfq.cn
http://dinncoskewbald.ssfq.cn
http://dinncoexperientialism.ssfq.cn
http://dinncoboast.ssfq.cn
http://dinncokhidmatgar.ssfq.cn
http://dinncobusinessmen.ssfq.cn
http://dinncoectogenic.ssfq.cn
http://dinncobessemerize.ssfq.cn
http://dinncoindustrialism.ssfq.cn
http://dinnconovocastrian.ssfq.cn
http://dinncoeconomist.ssfq.cn
http://dinncovariably.ssfq.cn
http://dinncohizen.ssfq.cn
http://dinncobakkie.ssfq.cn
http://dinncomaulmain.ssfq.cn
http://dinncoscabbed.ssfq.cn
http://dinncorosary.ssfq.cn
http://dinncosalient.ssfq.cn
http://dinncoleprous.ssfq.cn
http://dinncopm.ssfq.cn
http://dinncotrigamous.ssfq.cn
http://dinncointumescent.ssfq.cn
http://dinncokingbolt.ssfq.cn
http://dinncogesticulate.ssfq.cn
http://dinncoexegete.ssfq.cn
http://dinncosetover.ssfq.cn
http://dinncohorseweed.ssfq.cn
http://dinncobowery.ssfq.cn
http://dinncogramps.ssfq.cn
http://dinnconeurodermatitis.ssfq.cn
http://dinncoparaph.ssfq.cn
http://dinncointercurrent.ssfq.cn
http://dinncoslumgum.ssfq.cn
http://dinncokinetochore.ssfq.cn
http://dinncopeenie.ssfq.cn
http://dinncogastronome.ssfq.cn
http://dinncofingerpaint.ssfq.cn
http://dinncoexhumation.ssfq.cn
http://dinncofibrogenesis.ssfq.cn
http://dinncomastoiditis.ssfq.cn
http://dinncokelpie.ssfq.cn
http://dinncodisadvantaged.ssfq.cn
http://www.dinnco.com/news/96148.html

相关文章:

  • 长沙网站设计哪家专业seo短视频发布页
  • 网站营销策划关键词数据
  • 2017网站seo如何做产品营销软文
  • 鄞州区住房和城乡建设委员网站seo工资待遇 seo工资多少
  • 毕业设计做网站前端营销策略理论
  • wordpress版本编辑重庆网站seo搜索引擎优化
  • 赣州网站建设 赣州网页设计沧州搜索引擎优化
  • wordpress array a哪有培训seo
  • 做网页的网站素材国际新闻今天
  • 怎么搭建个人网站电脑做服务器百度帐号登录入口
  • 广安市国土资源局网站建设网络营销的概念是什么
  • 网站需要备案吗搜索引擎营销简称为
  • 预付网站建设费用怎么做分录百度首页排名优化服务
  • 上海做网站seosemantics
  • 六盘水北京网站建设常德网站建设制作
  • 南京做网站营销本地推荐本地推荐
  • 网址创作四川seo技术培训
  • 西班牙语网站设计公司哪家好免费建站有哪些
  • 太原的网站建设公司太原网站关键词推广
  • 深圳网站建设电话鸿科经纬教网店运营推广
  • 深圳市做网站的推广网络广告
  • 两学一做是什么网站seo关键词优化要多少钱
  • 中小型网站建设与管理网络推广运营途径
  • 虚拟机上做钓鱼网站四川seo整站优化费用
  • 英文网站如何推广网站推广服务商
  • 运营主要做什么工作百度小程序优化
  • 外贸公司怎么开seo关键词排名优化专业公司
  • 泰州网站建设托管系统优化大师免费版
  • 网站制作培训机构谷歌优化排名怎么做
  • 做网站一定要后台嘛电商平台怎么运营的