下面列出了org.springframework.web.bind.annotation.RequestMapping#path ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String getClassMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping mapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (mapping == null) {
return "/";
}
String[] paths = mapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + controllerType.getName());
}
return paths[0];
}
private static String getMethodMapping(Method method) {
Assert.notNull(method, "'method' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + method.toGenericString());
}
return paths[0];
}
private static String getClassMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping mapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (mapping == null) {
return "/";
}
String[] paths = mapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + controllerType.getName());
}
return paths[0];
}
private static String getMethodMapping(Method method) {
Assert.notNull(method, "'method' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + method.toGenericString());
}
return paths[0];
}
private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (requestMapping == null) {
return "/";
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
}
return paths[0];
}
private static String getMethodRequestMapping(Method method) {
Assert.notNull(method, "'method' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
}
return paths[0];
}
@VisibleForTesting
String[] paths(Class<?> controller)
{
RequestMapping annotation = AnnotationUtils.findAnnotation(controller, RequestMapping.class);
if (annotation != null)
{
return annotation.path();
}
return new String[] {};
}
private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (requestMapping == null) {
return "/";
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
}
return paths[0];
}
private static String getMethodRequestMapping(Method method) {
Assert.notNull(method, "'method' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
}
return paths[0];
}
private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (requestMapping == null) {
return "/";
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on controller {}, using first one", controllerType.getName());
}
return paths[0];
}
private static String getMethodRequestMapping(Method method) {
Assert.notNull(method, "'method' must not be null");
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on method {}, using first one", method.toGenericString());
}
return paths[0];
}
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String basePath = "";
RequestMapping basePathRequestMapping = objInst.getClass().getAnnotation(RequestMapping.class);
if (basePathRequestMapping != null) {
if (basePathRequestMapping.value().length > 0) {
basePath = basePathRequestMapping.value()[0];
} else if (basePathRequestMapping.path().length > 0) {
basePath = basePathRequestMapping.path()[0];
}
}
EnhanceRequireObjectCache enhanceRequireObjectCache = new EnhanceRequireObjectCache();
enhanceRequireObjectCache.setPathMappingCache(new PathMappingCache(basePath));
objInst.setSkyWalkingDynamicField(enhanceRequireObjectCache);
}
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String basePath = "";
RequestMapping basePathRequestMapping = objInst.getClass().getAnnotation(RequestMapping.class);
if (basePathRequestMapping != null) {
if (basePathRequestMapping.value().length > 0) {
basePath = basePathRequestMapping.value()[0];
} else if (basePathRequestMapping.path().length > 0) {
basePath = basePathRequestMapping.path()[0];
}
}
EnhanceRequireObjectCache enhanceRequireObjectCache = new EnhanceRequireObjectCache();
enhanceRequireObjectCache.setPathMappingCache(new PathMappingCache(basePath));
objInst.setSkyWalkingDynamicField(enhanceRequireObjectCache);
}
@Override
public JaxrsApplicationParser.Result tryParse(SourceType<?> sourceType) {
if (!(sourceType.type instanceof Class<?>)) {
return null;
}
final Class<?> cls = (Class<?>) sourceType.type;
// application
final SpringBootApplication app = AnnotationUtils.findAnnotation(cls, SpringBootApplication.class);
if (app != null) {
if (settings.scanSpringApplication) {
TypeScriptGenerator.getLogger().verbose("Scanning Spring application: " + cls.getName());
final ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(settings.classLoader);
final SpringApplicationHelper springApplicationHelper = new SpringApplicationHelper(settings.classLoader, cls);
final List<Class<?>> restControllers = springApplicationHelper.findRestControllers();
return new JaxrsApplicationParser.Result(restControllers.stream()
.map(controller -> new SourceType<Type>(controller, cls, "<scanned>"))
.collect(Collectors.toList())
);
} finally {
Thread.currentThread().setContextClassLoader(originalContextClassLoader);
}
} else {
return null;
}
}
// controller
final Component component = AnnotationUtils.findAnnotation(cls, Component.class);
if (component != null) {
TypeScriptGenerator.getLogger().verbose("Parsing Spring component: " + cls.getName());
final JaxrsApplicationParser.Result result = new JaxrsApplicationParser.Result();
final RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(cls, RequestMapping.class);
final String path = requestMapping != null && requestMapping.path() != null && requestMapping.path().length != 0 ? requestMapping.path()[0] : null;
final JaxrsApplicationParser.ResourceContext context = new JaxrsApplicationParser.ResourceContext(cls, path);
parseController(result, context, cls);
return result;
}
return null;
}
如果文章对你有帮助,欢迎点击上方按钮打赏作者