【问题标题】:Not able to map the url in controller无法在控制器中映射 url
【发布时间】:2012-02-22 07:40:55
【问题描述】:

我有http://localhost:8080/api/create/之类的请求网址,控制器有以下代码

@RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<String> getApiResponse(HttpServletRequest request)
            throws Exception {}

这个方法怎么控制呢?我在春天有任何方法可以做到这一点,因为我希望请求映射 url 仅为“/”

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    应该是:

    @RequestMapping(value = "/create", method = RequestMethod.GET)

    【讨论】:

      【解决方案2】:

      请求映射的重点是将每个方法映射到不同的 url。在你的情况下:

      @RequestMapping(value="/api/create")
      

      【讨论】:

        【解决方案3】:

        如果您的 Web 应用程序是您的应用程序服务器上的默认 Web 应用程序,即您不必在 URL 中提及应用程序本身的路径,您必须说

        @RequestMapping(value = "/*", method = RequestMethod.GET)

        @RequestMapping(value = "/api/create/", method = RequestMethod.GET)

        或者在类上放@RequestMapping注解: @RequestMapping(value = "/api/", method = RequestMethod.GET) 然后用注解标记getApiResponse方法 @RequestMapping(value = "/create/", method = RequestMethod.GET)

        您还可以在一个@RequestMapping 注释中提及多个网址:

        @RequestMapping(value = {"/api/", "/api", "/api/*", "/api/create/"})

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 2016-09-17
        • 2016-03-22
        • 2015-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多