2016-05-02 19 views
0

私のカスタム注釈によって注釈が付けられたコントローラのメソッドがあります。私がそれらを選択すると、メソッドのリストを受け取り、それぞれのメソッドのURLを取得したいと思います。例えばSpring MVC取得メソッドURL

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

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

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

} 

そして、私は受け取るしたい:

をmethodOneために - methodTwo用/テスト

- 多分このような/テスト/ 2

+0

は、あなたの質問は何である –

+0

私は春からそれらのURLを取得できますか?マッピング? – Nikita

+0

多分、ここで提供される答えはあなたが望むことをしていますか?https://stackoverflow.com/questions/10898056/how-to-find-all-controllers-in-spring-mvc/10899118#10899118 – lumue

答えて

0

何か。 まず2つの方法のマッピング3 URLの持つコントローラの書き込み:RequestMappingHandlerMapping Beanを作成し、あなたの@Configurationクラスに続いて

@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 
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の提供)次のビューを提供します:公共org.springframework.web.servlet.ModelAndViewの組織:Spring MVCのURLの

Javaメソッドの

ドキュメント。 springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(のjavax.servlet.http.HttpServletRequest、javax.servlet.http.HttpServletResponseの)、URL:[/エラー]

JAVA方法:公共のjava.lang.Stringコム。デモ.DemoController.firstMethod(org.springframework.ui.Model)、URL:[/ test]

JAVA方法:公共org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(するjavax.servlet.http.HttpServletRequest)、URL:[/エラー]

JAVAメソッド:public java.lang.String com.demo.DemoController.secondMethod(org.springframework.ui.Model)、URL:[/ test/demo || /テスト/ DEMO1]

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

+0

正確には私が望むものではありませんが、そのアイデアは私の問題解決に役立ちます。どうも – Nikita

関連する問題