Skip to content

Commit 109d985

Browse files
awmeinemasnicoll
authored andcommitted
Allow AbstractUrlHandlerMapping to add/remote handlers
See gh-32064
1 parent a7ceedf commit 109d985

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,14 @@ protected void registerHandler(String[] urlPaths, String beanName) throws BeansE
404404

405405
/**
406406
* Register the specified handler for the given URL path.
407+
* <p>This method may be invoked at runtime after initialization has completed.
407408
* @param urlPath the URL the bean should be mapped to
408409
* @param handler the handler instance or handler bean name String
409410
* (a bean name will automatically be resolved into the corresponding handler bean)
410411
* @throws BeansException if the handler couldn't be registered
411412
* @throws IllegalStateException if there is a conflicting handler registered
412413
*/
413-
protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
414+
public void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
414415
Assert.notNull(urlPath, "URL path must not be null");
415416
Assert.notNull(handler, "Handler object must not be null");
416417
Object resolvedHandler = handler;
@@ -456,6 +457,39 @@ else if (urlPath.equals("/*")) {
456457
}
457458
}
458459

460+
/**
461+
* Un-register the given mapping.
462+
* <p>This method may be invoked at runtime after initialization has completed.
463+
* @param urlPath the mapping to unregister
464+
*/
465+
public void unregisterHandler(String urlPath) throws IllegalArgumentException {
466+
Assert.notNull(urlPath, "URL path must not be null");
467+
Object mappedHandler = this.handlerMap.get(urlPath);
468+
if (mappedHandler != null) {
469+
if (urlPath.equals("/")) {
470+
if (logger.isTraceEnabled()) {
471+
logger.trace("Unregistered root mapping.");
472+
}
473+
setRootHandler(null);
474+
}
475+
else if (urlPath.equals("/*")) {
476+
if (logger.isTraceEnabled()) {
477+
logger.trace("Unregistered default mapping.");
478+
}
479+
setDefaultHandler(null);
480+
}
481+
else {
482+
if (logger.isTraceEnabled()) {
483+
logger.trace("Unregistered mapping \"" + urlPath + "\"");
484+
}
485+
this.handlerMap.remove(urlPath);
486+
if(getPatternParser() != null) {
487+
this.pathPatternHandlerMap.remove(getPatternParser().parse(urlPath));
488+
}
489+
}
490+
}
491+
}
492+
459493
private String getHandlerDescription(Object handler) {
460494
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
461495
}

0 commit comments

Comments
 (0)