关于SpringMVC中WebApplicationContext扫描不到指定Mapping

场景:虽然controller中写了mapping,但是WebApplicationContext怎么都扫描不到

解决:可以看看SpringMVC配置中,是否将你所在的controller扫描进去了。XML配置如下

1
2
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.hyyt.shopjsp.**.controller"/>

这行配置表示,它扫描com.hyyt.shopjsp类下controller包中

另外附上以JSON形式输出项目方法名称,方法URL和controller名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Autowired
WebApplicationContext appContext;
@RequestMapping(value = "/showAllMappingInfo", method = RequestMethod.GET)
@ResponseBody
public JSONArray index(HttpServletRequest request)
{

//请求url和处理方法的映射
JSONArray requestToMethodItemJSONArray = new JSONArray();

//获取所有的RequestMapping
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext,
HandlerMapping.class, true, false);

for (HandlerMapping handlerMapping : allRequestMappings.values())
{
//本项目只需要RequestMappingHandlerMapping中的URL映射
if (handlerMapping instanceof RequestMappingHandlerMapping)
{
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
{
RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();

RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();

PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
String requestUrl = patternsCondition.getPatterns().toString();

String controllerName = mappingInfoValue.getBeanType().toString();
String requestMethodName = mappingInfoValue.getMethod().getName();
Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();


JSONObject jsonObject = new JSONObject();
jsonObject.put("requestUrl", requestUrl);
jsonObject.put("controllerName", controllerName);
jsonObject.put("requestMethodName", requestMethodName);

requestToMethodItemJSONArray.add(jsonObject);
}
break;
}
}
return requestToMethodItemJSONArray;
}

返回信息如下

赏个🍗吧
0%