首页 > 其他分享 >8.12 SpringBoot集成ElasticSearch之拦截器

8.12 SpringBoot集成ElasticSearch之拦截器

时间:2022-10-24 14:35:48浏览次数:59  
标签:interceptor 拦截器 employeeId ElasticSearch log Override Interceptor public SpringB


1.简介
eBatis中拦截器的加载通过SPI方式实现,只需要提供的目标类实现io.manbang.ebatis.core.interceptor.Interceptor接口,在目标类上加上注解@AutoService(Interceptor.class),由auto-service生成。

2.interceptor开发
在项目目录“/src/main/java/com/example/es”下新建interceptor目录,并在interceptor目录下新建EmployeeInterceptor类,EmployeeInterceptor类需要实现Interceptor接口,具体代码如下。

@Slf4j
@AutoService(Interceptor.class)
public class EmployeeInterceptor implements Interceptor {
@Override
public int getOrder() {
return 0;
}

@Override
public void handleException(Throwable throwable) {
log.error("throwable:{}", throwable);
}

@Override
public <T extends ActionRequest> void postRequest(RequestInfo<T>) {
log.info("requestInfo1:{}", requestInfo);
log.info("requestInfo2:{}", requestInfo.actionRequest());
log.error("requestInfo employeeId:{}", ContextHolder.getString("employeeId"));
}

@Override
public <T extends ActionRequest> void preResponse(PreResponseInfo<T>) {
log.error("preResponseInfo employeeId:{}", ContextHolder.getString("employeeId"));
}

@Override
public <T extends ActionRequest, R extends ActionResponse> void postResponse(PostResponseInfo<T, R>) {
log.info("postResponseInfo:{}", postResponseInfo.actionResponse());
log.error("postResponseInfo employeeId:{}", ContextHolder.<String>getValue("employeeId"));
}
}

3.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后可在控制台输出如下的信息。


标签:interceptor,拦截器,employeeId,ElasticSearch,log,Override,Interceptor,public,SpringB
From: https://blog.51cto.com/u_15843693/5789671

相关文章

  • 17.Linux下安装ElasticSearch
    1.解压解压安装包并将解压后的目录移动到/usr/local/elasticsearch目录下。tar-zxvfelasticsearch-7.5.1.tgzmvelasticsearch-7.5.1/usr/local/elasticsearchcd2.配置......
  • springboot 集成consul,实现服务注册案例
     网上关于这方便的集成很多,实际自己搭建时候,可能会有一些问题,在这总结一下,容易出错的地方,我会加特殊标记本集成使用springboot版本:2.3.3.RELEASE,springcloud......
  • SpringBoot集成feign的方法分享
    转自:http://www.java265.com/JavaFramework/SpringBoot/202205/3532.html 下文笔者讲述SpringBoot集成feign的方法分享,如下所示实现思路:1.引入相应的jar包......
  • 拦截器
    1.拦截器作用拦截控制方法的执行2.实现packagecom.cj.rest.interceptors;importorg.springframework.stereotype.Component;importorg.springframework.web.......
  • kafka springBoot 报错 not present and missingTopicsFatal is true kafka missing-t
    这个问题可以追溯到springboot和kafka的版本问题,解决这个问题太麻烦,要去看官方文档,我选择不看。这里提供一种通用的解决方式在kafkaConfig配置文件中添加下面的代码/**......
  • SpringBoot全局对象数据访问
    创建一个测试类,添加@ControllerAdvie和@ModelAttribute注解packagecom.model.test;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.......
  • SpringBoot启动过程(一)
    1,创建SpringApplication实例1.1设置WebApplicationType(应用类型)目前有三种类型,SERVLET或者REACTIVE或者NONE判断是根据某些特定的类是否存在来判断的,具体推断方法为deduceF......
  • SpringBoot项目整合Mybatis时Mapper.xml文件的存放位置
    SpringBoot项目整合Mybatis时Mapper.xml文件的存放位置目录:方式一:放在与Mapper接口同级目录方式二:在resources创建Mapper接口同名文件夹用来存放Mapper.xml文件方式三:......
  • SpringBoot中https的配置
    配置#https://help.aliyun.com/document_detail/365559.html#HTTPS协议默认端口号为443,需要使用其他端口时,您可以在此处自定义。server.port=8002#https://docs.ora......
  • ElasticSearch之Quick.ElasticSearch.Furion组件的使用
    ElasticSearch使用说明本章,我们主要讲解在.Net中对Quick.ElasticSearch.Furion的使用进行介绍!ElasticSearch的官方客户端API文档地址:https://www.elastic.co/guid......