获取swagger 上url 和方法名(spring boot )
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取swagger 上url 和方法名(spring boot )相关的知识,希望对你有一定的参考价值。
参考技术A @Testpublic void listComboCourse()
List> resultList =new ArrayList<>();
//RequestMappingHandlerMapping requestMappingHandlerMapping =
// applicationContext.getBean(RequestMappingHandlerMapping.class);
RequestMappingHandlerMapping requestMappingHandlerMapping =
(RequestMappingHandlerMapping)applicationContext.getBean("requestMappingHandlerMapping");
// 获取url与类和方法的对应信息
Map map = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry mappingInfoHandlerMethodEntry : map.entrySet())
Map resultMap =new LinkedHashMap<>();
RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();
resultMap.put("className",handlerMethod.getMethod().getDeclaringClass().getName()); // 类名
Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
for (Annotation annotation : parentAnnotations)
if (annotationinstanceof Api)
Api api = (Api) annotation;
resultMap.put("classDesc",api.value());
resultMap.put("functionDesc",api.tags()[0].toString());//接口描述
else if (annotationinstanceof RequestMapping)
RequestMapping requestMapping = (RequestMapping) annotation;
if (null != requestMapping.value() && requestMapping.value().length >0)
resultMap.put("classURL",requestMapping.value()[0]);//类URL
resultMap.put("methodName", handlerMethod.getMethod().getName()); // 方法名
Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
if (annotations !=null)
// 处理具体的方法信息
for (Annotation annotation : annotations)
if (annotationinstanceof ApiOperation)
ApiOperation methodDesc = (ApiOperation) annotation;
String desc = methodDesc.value();
resultMap.put("methodDesc",desc);//接口描述
PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
for (String url : p.getPatterns())
if(!url.contains("app"))
resultMap.put("methodURL",url);//请求URL
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods())
resultMap.put("requestType",requestMethod.toString());//请求方式:POST/PUT/GET/DELETE
resultList.add(resultMap);
System.out.println(JSON.toJSON(resultList));
resultList.forEach(a->
);
swagger@GetMapping参数
应使用
1.@ApiImplicitParams:用在请求的方法上,包含一组参数说明
2.@ApiImplicitParam:用在 @ApiImplicitParams 注解中或在请求的方法上,指定一个请求参数的配置信息
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值
以上是关于获取swagger 上url 和方法名(spring boot )的主要内容,如果未能解决你的问题,请参考以下文章