提交 2c822363 编写于 作者: S SevenPointOld 提交者: wu-sheng

Spring mvc plugin auto add '/' if necessary (#1315)

* spring mvc plugin auto add '/' if necessary
上级 5ba4f647
...@@ -54,7 +54,7 @@ public class ControllerConstructorInterceptorTest { ...@@ -54,7 +54,7 @@ public class ControllerConstructorInterceptorTest {
Method m = obj.getClass().getMethods()[0]; Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString"); cache.addPathMapping(m, "#toString");
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1#toString"); Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1/#toString");
} }
@Test @Test
...@@ -67,7 +67,7 @@ public class ControllerConstructorInterceptorTest { ...@@ -67,7 +67,7 @@ public class ControllerConstructorInterceptorTest {
Method m = obj.getClass().getMethods()[0]; Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString"); cache.addPathMapping(m, "#toString");
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "#toString"); Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/#toString");
} }
@Test @Test
...@@ -80,7 +80,7 @@ public class ControllerConstructorInterceptorTest { ...@@ -80,7 +80,7 @@ public class ControllerConstructorInterceptorTest {
Method m = obj.getClass().getMethods()[0]; Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString"); cache.addPathMapping(m, "#toString");
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3#toString"); Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3/#toString");
} }
@RequestMapping(value = "/test1") @RequestMapping(value = "/test1")
......
...@@ -45,7 +45,31 @@ public class PathMappingCacheTest { ...@@ -45,7 +45,31 @@ public class PathMappingCacheTest {
Method m = obj.getClass().getMethods()[0]; Method m = obj.getClass().getMethods()[0];
pathMappingCache.addPathMapping(m, "#toString"); pathMappingCache.addPathMapping(m, "#toString");
Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "org.apache.skywalking.apm.plugin.spring.mvc#toString"); Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "/org.apache.skywalking.apm.plugin.spring.mvc/#toString");
} }
@Test
public void testAutoAddPathSeparator() {
String rightPath = "/root/sub";
Object obj = new Object();
Method m = obj.getClass().getMethods()[0];
PathMappingCache cache = new PathMappingCache("root");
cache.addPathMapping(m, "sub");
Assert.assertEquals(cache.findPathMapping(m), rightPath);
PathMappingCache cache2 = new PathMappingCache("/root");
cache2.addPathMapping(m, "/sub");
Assert.assertEquals(cache2.findPathMapping(m), rightPath);
PathMappingCache cache3 = new PathMappingCache("root");
cache3.addPathMapping(m, "/sub");
Assert.assertEquals(cache3.findPathMapping(m), rightPath);
PathMappingCache cache4 = new PathMappingCache("/root");
cache4.addPathMapping(m, "sub");
Assert.assertEquals(cache4.findPathMapping(m), rightPath);
}
} }
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons; package org.apache.skywalking.apm.plugin.spring.mvc.commons;
import org.apache.skywalking.apm.util.StringUtil;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -28,11 +30,17 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -28,11 +30,17 @@ import java.util.concurrent.ConcurrentHashMap;
* @author zhangxin * @author zhangxin
*/ */
public class PathMappingCache { public class PathMappingCache {
private static final String PATH_SEPARATOR = "/";
private String classPath = ""; private String classPath = "";
private ConcurrentHashMap<Method, String> methodPathMapping = new ConcurrentHashMap<Method, String>(); private ConcurrentHashMap<Method, String> methodPathMapping = new ConcurrentHashMap<Method, String>();
public PathMappingCache(String classPath) { public PathMappingCache(String classPath) {
if (!StringUtil.isEmpty(classPath) && !classPath.startsWith(PATH_SEPARATOR)) {
classPath = PATH_SEPARATOR + classPath;
}
this.classPath = classPath; this.classPath = classPath;
} }
...@@ -41,6 +49,10 @@ public class PathMappingCache { ...@@ -41,6 +49,10 @@ public class PathMappingCache {
} }
public void addPathMapping(Method method, String methodPath) { public void addPathMapping(Method method, String methodPath) {
if (!StringUtil.isEmpty(methodPath) && !methodPath.startsWith(PATH_SEPARATOR)
&& !classPath.endsWith(PATH_SEPARATOR)) {
methodPath = PATH_SEPARATOR + methodPath;
}
methodPathMapping.put(method, classPath + methodPath); methodPathMapping.put(method, classPath + methodPath);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册