From 90d25bd582a2944e4609d0be8f91c2048b2b1cc0 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 12 Nov 2014 15:45:44 +0000 Subject: [PATCH] Use Set (again) for enumerating MvcEndpoints This reverts a change that I assume was orphaned from work in progress to address #1353, but is no longer needed. Anyway there is no reason to restrict MvcEndpoints to be unique by path since they can declare their own @RequestMappings (and if there are duplicates they will be detected when those are scanned). Fixes gh-1911 --- .../endpoint/mvc/EndpointHandlerMapping.java | 20 ++++--------------- .../mvc/EndpointHandlerMappingTests.java | 15 ++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java index a78d724ea40..c9854a82b67 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java @@ -18,10 +18,7 @@ package org.springframework.boot.actuate.endpoint.mvc; import java.lang.reflect.Method; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Set; import org.springframework.boot.actuate.endpoint.Endpoint; @@ -53,7 +50,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl public class EndpointHandlerMapping extends RequestMappingHandlerMapping implements ApplicationContextAware { - private final Map endpoints; + private final Set endpoints; private String prefix = ""; @@ -65,26 +62,17 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme * @param endpoints */ public EndpointHandlerMapping(Collection endpoints) { - this.endpoints = buildEndpointsMap(endpoints); + this.endpoints = new HashSet(endpoints); // By default the static resource handler mapping is LOWEST_PRECEDENCE - 1 // and the RequestMappingHandlerMapping is 0 (we ideally want to be before both) setOrder(-100); } - private Map buildEndpointsMap( - Collection endpoints) { - Map map = new LinkedHashMap(); - for (MvcEndpoint endpoint : endpoints) { - map.put(endpoint.getPath(), endpoint); - } - return Collections.unmodifiableMap(map); - } - @Override public void afterPropertiesSet() { super.afterPropertiesSet(); if (!this.disabled) { - for (MvcEndpoint endpoint : this.endpoints.values()) { + for (MvcEndpoint endpoint : this.endpoints) { detectHandlerMethods(endpoint); } } @@ -183,7 +171,7 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme * Return the endpoints */ public Set getEndpoints() { - return new HashSet(this.endpoints.values()); + return new HashSet(this.endpoints); } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMappingTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMappingTests.java index ff32ad2b114..761e1947f87 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMappingTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMappingTests.java @@ -133,6 +133,21 @@ public class EndpointHandlerMappingTests { nullValue()); } + @Test + public void duplicatePath() throws Exception { + TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("/a")); + TestActionEndpoint other = new TestActionEndpoint(new TestEndpoint("/a")); + EndpointHandlerMapping mapping = new EndpointHandlerMapping(Arrays.asList( + endpoint, other)); + mapping.setDisabled(true); + mapping.setApplicationContext(this.context); + mapping.afterPropertiesSet(); + assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a")), + nullValue()); + assertThat(mapping.getHandler(new MockHttpServletRequest("POST", "/a")), + nullValue()); + } + private static class TestEndpoint extends AbstractEndpoint { public TestEndpoint(String path) {