1. springboot启动打印controller映射url日志
2023-10-11 14:09:46.482 TRACE 27244 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.d.c.j.w.c.LabelConfigController:
{POST [/labelConfig/add]}: add(LabelConfig)
{POST [/labelConfig/update]}: update(LabelConfig)
{GET [/labelConfig/delete]}: delete(Integer)
{POST [/labelConfig/list]}: list(LabelConfig)
{GET [/labelConfig/detail]}: detail(Integer)
{POST [/labelConfig/page]}: page(LabelConfig,Page)
2023-10-11 14:09:46.486 TRACE 27244 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :
o.s.b.a.w.s.e.BasicErrorController:
{ [/error]}: error(HttpServletRequest)
{ [/error], produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
2. 分析AbstractHandlerMethodMapping源码
protected void detectHandlerMethods(Object handler) {
Class<?> handlerType = (handler instanceof String ?
obtainApplicationContext().getType((String) handler) : handler.getClass());
if (handlerType != null) {
Class<?> userType = ClassUtils.getUserClass(handlerType);
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
(MethodIntrospector.MetadataLookup<T>) method -> {
try {
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
});
if (logger.isTraceEnabled()) {
logger.trace(formatMappings(userType, methods));
}
else if (mappingsLogger.isDebugEnabled()) {
mappingsLogger.debug(formatMappings(userType, methods));
}
methods.forEach((method, mapping) -> {
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
registerHandlerMethod(handler, invocableMethod, mapping);
});
}
}
注意:低版本spring-webmvc-5.2.13.RELEASE.jar没有
else if (mappingsLogger.isDebugEnabled()) {
mappingsLogger.debug(formatMappings(userType, methods));
}
3. yml配置日志
logging:
level:
root: INFO
# 低版本spring-webmvc-5.2.13.RELEASE.jar
org.springframework.web: TRACE
# 高版本spring-webmvc-5.3.13.jar
# _org.springframework.web: DEBUG
开启 _org.springframework.web: DEBUG 时日志
2023-10-11 14:23:54.649 DEBUG 30144 --- [ main] _.s.web.servlet.HandlerMapping.Mappings :
c.d.c.j.w.c.LabelConfigController:
{POST [/labelConfig/add]}: add(LabelConfig)
{POST [/labelConfig/update]}: update(LabelConfig)
{GET [/labelConfig/delete]}: delete(Integer)
{POST [/labelConfig/list]}: list(LabelConfig)
{GET [/labelConfig/detail]}: detail(Integer)
{POST [/labelConfig/page]}: page(LabelConfig,Page)
2023-10-11 14:23:54.653 DEBUG 30144 --- [ main] _.s.web.servlet.HandlerMapping.Mappings :
o.s.b.a.w.s.e.BasicErrorController:
{ [/error]}: error(HttpServletRequest)
{ [/error], produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)