2016-05-02 104 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

我怎樣才能從春天的網址嗎?映射? – Nikita

+0

也許這裏提供的答案做你想要什麼?https://stackoverflow.com/questions/10898056/how-to-find-all-controllers-in-spring-mvc/10899118#10899118 – lumue

回答

0

事情是這樣的,也許。 先寫有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豆:

@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方法的文檔:公共org.springframework.web.servlet.ModelAndView組織。 springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse),URL:[/錯誤]

JAVA方法:public java.lang.String中融爲一體。 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:[/錯誤]

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

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

+0

不完全是我想要的,但是這個想法幫助我解決了我的問題。謝謝 – Nikita