【问题标题】:Spring MVC get method URLSpring MVC 获取方法 URL
【发布时间】:2016-05-02 10:31:15
【问题描述】:

我的控制器方法由我的自定义注释注释。当我选择它们时,我会收到方法列表,并且我想获取每个方法的 URL。

例如:

@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping(method = RequestMethod.GET)
    public String methodOne(){}

    @RequestMapping(value = "/2", method = RequestMethod.GET)
    public String methodTwi(){}

}

我想收到:

对于methodOne - /test

对于方法二 - /test/2

【问题讨论】:

  • 你有什么问题?
  • 如何从 spring 映射中获取这些 url?
  • 也许这里提供的答案是你想要的? stackoverflow.com/questions/10898056/…
  • 不完全是。我不想找到所有的控制器。如果存在,我只想按方法获取方法 url。 RequestMappingHandlerMapping 中有一个方法叫 getMappingForMethod,但它是受保护的。

标签: java spring spring-mvc annotations


【解决方案1】:

可能是这样的。 首先用 2 个方法映射 3 个 URL 编写一个控制器:

@Controller
@RequestMapping("/test")
public class DemoController {

    @RequestMapping(method = RequestMethod.GET)
    public String firstMethod(Model model) {
        model.addAttribute("name", "Olivier");
        return "firstPage";
    }

    @RequestMapping(method = RequestMethod.GET, value = {"/demo", "/demo1"})
    public String secondMethod(Model model) {
        model.addAttribute("name", "Olivier");
        return "secondPage";
    }
}

然后在您的 @Configuration 类中,创建 RequestMappingHandlerMapping bean:

@Configuration
public class DemoSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoSpringApplication.class, args);
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
        return mapping;
    }
}

创建一个反转地图的特殊控制器:

@Controller
@RequestMapping("/requests")
public class RequestMappingController {
    @Autowired
    private RequestMappingHandlerMapping handler;

    @RequestMapping(method = RequestMethod.GET)
    public String showDoc(Model model) {
        model.addAttribute("methods", handler.getHandlerMethods());
        Map<RequestMappingInfo, HandlerMethod> map = handler.getHandlerMethods();

        Set<RequestMappingInfo> mappings = map.keySet();
        Map<String, String> reversedMap = new HashMap<String, String>();
        for(RequestMappingInfo info : mappings) {
            HandlerMethod method = map.get(info);
            reversedMap.put(method.toString(), info.getPatternsCondition().toString());
        }
        model.addAttribute("methods", reversedMap);

        return "showDoc";
    }
}

在showDoc页面中,遍历新建的地图:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Documentation</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Documentation of Spring MVC URL's</h1>
    <p th:each="methods: ${methods}">
        <p><span th:text="'JAVA method:' + ${methods.key} + ', URL:' + ${methods.value}"></span></p>
    </p>
</body>
</html>

这将给出以下视图(按方法排序并提供映射的 URL):

Spring MVC URL 的文档

JAVA方法:public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse),URL:[ /错误]

JAVA方法:public java.lang.String com.demo.DemoController.firstMethod(org.springframework.ui.Model),URL:[/test]

JAVA方法:public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest), URL:[/error]

JAVA方法:public java.lang.String com.demo.DemoController.secondMethod(org.springframework.ui.Model), URL:[/test/demo || /test/demo1]

JAVA方法:public java.lang.String com.demo.RequestMappingController.showDoc(org.springframework.ui.Mode

【讨论】:

  • 不完全是我想要的,但这个想法帮助我解决了我的问题。谢谢
猜你喜欢
  • 1970-01-01
  • 2010-12-02
  • 2011-06-28
  • 2011-08-01
  • 2018-04-03
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
相关资源
最近更新 更多