spring mvc 获取所有请求映射 [英] spring mvc get all request mappings

查看:52
本文介绍了spring mvc 获取所有请求映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个相当大的 spring mvc web 应用程序,其中控制器用 @Controller 注释,方法用 @RequestMapping 注释.

We have a quite big spring mvc web application where the controllers are annotated with @Controller and the methods with @RequestMapping.

我现在想创建一个测试,检查每个可能的 url 并检查返回值是否为 200.

I would like to create a test now which checks every possible url and checks if the return value is 200.

是否有可能从 spring 中获取所有映射?

Is it somehow possible to get all the mappings from spring ?

推荐答案

我正在复制我的一个 以前的答案在这里:

I am replicating one of my previous answers here:

如果你使用的是 Spring 3.1,这个 handlerMapping 组件是 RequestMappingHandlerMapping 的一个实例,你可以查询它以找到 handlerMappedMethods 和相关的控制器,沿着这些行(如果你使用的是旧版本的 Spring,你应该能够使用类似的方法):

If you are using Spring 3.1 this handlerMapping component is an instance of RequestMappingHandlerMapping, which you can query to find the handlerMappedMethods and the associated controllers, along these lines(if you are on an older version of Spring, you should be able to use a similar approach):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class EndpointDocController {
 private final RequestMappingHandlerMapping handlerMapping;

 @Autowired
 public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {
  this.handlerMapping = handlerMapping;
 }

 @RequestMapping(value="/endpointdoc", method=RequestMethod.GET)
 public void show(Model model) {
  model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());
 } 
}

我在这个 url http://biju-allandsundry.blogspot.com/2012/03/endpoint-documentation-controller-for.html

本文基于 Spring Source 的 Rossen Stoyanchev 关于 Spring 3.1 的演示.

This is based on a presentation on Spring 3.1 by Rossen Stoyanchev of Spring Source.

这篇关于spring mvc 获取所有请求映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆