在早期的版本,Spring映射接口总是在容器启动的时候会打印
logger.info("Mapped \"" + mapping + "\" onto " + handlerMethod);
就是说每一个接口映射完都会打印日志。
比如springBoot 整合spring-webmvc在5.0.5版本的时候就会使用org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry中register(T mapping, Object handler, Method method)
public void register(T mapping, Object handler, Method method) {
this.readWriteLock.writeLock().lock();
try {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
assertUniqueMethodMapping(handlerMethod, mapping);
if (logger.isInfoEnabled()) {
logger.info("Mapped \"" + mapping + "\" onto " + handlerMethod);
}
this.mappingLookup.put(mapping, handlerMethod);
List<String> directUrls = getDirectUrls(mapping);
for (String url : directUrls) {
this.urlLookup.add(url, mapping);
}
String name = null;
if (getNamingStrategy() != null) {
name = getNamingStrategy().getName(handlerMethod, mapping);
addMappingName(name, handlerMethod);
}
CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
if (corsConfig != null) {
this.corsLookup.put(handlerMethod, corsConfig);
}
this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls, name));
}
finally {
this.readWriteLock.writeLock().unlock();
}
}
打印出注册的接口。
会像这样打印
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping Line:547 -
Mapped "{[/mgr/edit]}" onto
public com.stylefeng.guns.core.base.tips.Tip com.stylefeng.guns.modular.system.controller.UserMgrController.edit
(com.stylefeng.guns.modular.system.transfer.UserDto,org.springframework.validation.BindingResult)
throws javax.naming.NoPermissionException
但是在spring-webmvc在5.1.8版本的时候org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry中register(T mapping, Object handler, Method method)
public void register(T mapping, Object handler, Method method) {
this.readWriteLock.writeLock().lock();
try {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
assertUniqueMethodMapping(handlerMethod, mapping);
this.mappingLookup.put(mapping, handlerMethod);
List<String> directUrls = getDirectUrls(mapping);
for (String url : directUrls) {
this.urlLookup.add(url, mapping);
}
String name = null;
if (getNamingStrategy() != null) {
name = getNamingStrategy().getName(handlerMethod, mapping);
addMappingName(name, handlerMethod);
}
CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
if (corsConfig != null) {
this.corsLookup.put(handlerMethod, corsConfig);
}
this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls, name));
}
finally {
this.readWriteLock.writeLock().unlock();
}
}
这个方法将不打印日志。
但是它会打印映射了多少接口,而且日志输出级别不是info,而是debug。代码如下:
logger.debug(total + " mappings in " + formatMappingName());
在哪里打印呢:
在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping中handlerMethodsInitialized(Map<T, HandlerMethod> handlerMethods)方法中打印。
代码如下:
/**
* Invoked after all handler methods have been detected.
* @param handlerMethods a read-only map with handler methods and mappings.
*/
protected void handlerMethodsInitialized(Map<T, HandlerMethod> handlerMethods) {
// Total includes detected mappings + explicit registrations via registerMapping
int total = handlerMethods.size();
if ((logger.isTraceEnabled() && total == 0) || (logger.isDebugEnabled() && total > 0) ) {
logger.debug(total + " mappings in " + formatMappingName());
}
}
所以小伙伴如果要看接口映射成功与否,就在这个地方打断点。
就像上面的图片所示:笔者木有写任何的接口,但是Springmvc会映射/error和/error,produces[text/html]接口。