美文网首页
springboot 2.1如何打印 RequestMappin

springboot 2.1如何打印 RequestMappin

作者: rock_fish | 来源:发表于2020-02-20 16:56 被阅读0次

    Spring Boot 2.1 之前 启动的时候日志使用INFO 级别 ,就可以打印 RequestMapping 的信息,示例如下:

    logging:
      level:
        root: info
    

    Spring Boot 2.1 之后的版本 使用以上配置,则无法打印出来RequestMapping信息,从源码:org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#detectHandlerMethods这个方法里可以看到这些信息的日志级别是TRACE

        protected void detectHandlerMethods(Object handler) {
            Class<?> handlerType = handler instanceof String ? this.obtainApplicationContext().getType((String)handler) : handler.getClass();
            if (handlerType != null) {
                Class<?> userType = ClassUtils.getUserClass(handlerType);
                Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (method) -> {
                    try {
                        return this.getMappingForMethod(method, userType);
                    } catch (Throwable var4) {
                        throw new IllegalStateException("Invalid mapping on handler class [" + userType.getName() + "]: " + method, var4);
                    }
                });
                if (this.logger.isTraceEnabled()) {
                    this.logger.trace(this.formatMappings(userType, methods));
                }
    
                methods.forEach((method, mapping) -> {
                    Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
                    this.registerHandlerMethod(handler, invocableMethod, mapping);
                });
            }
    
        }
    

    所以要打印Controller里面的 RequestMapping信息需要把org.springframework.web打印日志的格式设置为trace,示例如下:
    最简单的操作root设置为trace,

    logging:
      level:
        root: trace
    

    但是root设置为trace这样会显示出太多trace,debug信息,所以只为RequestMapping所对应的org.springframework.web里日志级别设置为trace:

    logging:
      level:
        org.springframework.web: trace
    

    日志显示的样式如下:

    [2020-02-20 16:49:56.056] TRACE 4066 [titans001-c0a8016e-439496-40026,null] [main] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] : 
        c.z.t.p.c.HttpController:
        {GET /http/get}: get(String,HttpServletRequest)
        {POST /http/update}: update(Object,HttpServletRequest)
        {DELETE /http/delete}: delete(Object,HttpServletRequest)
        {PUT /http/insert}: insert(Object,HttpServletRequest)
    

    相关文章

      网友评论

          本文标题:springboot 2.1如何打印 RequestMappin

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