【问题标题】:Spring Boot - Request Mapping won't accept trailing slash or multiple directoriesSpring Boot - 请求映射不接受斜杠或多个目录
【发布时间】:2018-07-03 01:46:53
【问题描述】:

当我的控制器中的 RequestMapping 时,我能够将一个 html 文件映射到“/”,另一个映射到“/users”。但是,尝试映射到“/users/”或“/users/test”是行不通的。在控制台中,它会说端点已映射,但在尝试访问它时,我会得到 404 错误页面。

package com.bridge.Bitter.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BitterController {

    //works
    @RequestMapping(value="/")
    public String getMainPage(){
        return "main.html";
    }

    //works
    @RequestMapping(value="/users")
    public String getUsersPage(){
        return "users.html";
    }

    //doesn't work, Whitelabel error page
    @RequestMapping(value="/users/")
    public String getUsersSlashPage(){
        return "users.html";
    }
    //doesn't work, Whitelabel error page
    @RequestMapping(value="/users/test")
    public String getUsersTestPage(){
        return "users.html";
    }

}

我的 application.properties 只包含“spring.data.rest.basePath=/api”。

如果我从@Controller 更改为@Rest Controller,会发生以下情况:

package com.bridge.Bitter.controllers;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class BitterController {

    //works
    @RequestMapping(value="/")
    public String getMainPage(){
        return "main.html";
    }

    //returns a webpage with the text "users.html" on it instead of serving the html
    @RequestMapping(value="/users")
    public String getUsersPage(){
        return "users.html";
    }

    //returns a webpage with the text "users.html" on it instead of serving the html
    @RequestMapping(value="/users/")
    public String getUsersSlashPage(){
        return "users.html";
    }
    //returns a webpage with the text "users.html" on it instead of serving the html
    @RequestMapping(value="/users/test")
    public String getUsersTestPage(){
        return "users.html";
    }

}

将函数从返回字符串更改为返回

new ModelAndView("user.html")

适用于 /users,但随后会出现 /users/ 和 /users/test 的 404 错误。

【问题讨论】:

    标签: java spring spring-mvc spring-boot request-mapping


    【解决方案1】:

    您正在尝试两次映射相同的路径。 /users 被视为与 /users/ 相同,因此 Spring 无法解析应由哪个控制器方法处理请求。

    你可以试试这个:

    package com.bridge.Bitter.controllers;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class BitterController {
    
        @RequestMapping("/")
        public String getMainPage(){
            return "main.html";
        }
    
        @RequestMapping("/users")
        public String getUsersPage(){
            return "users.html";
        }
    
        @RequestMapping("/users/test")
        public String getUsersTestPage(){
            return "users.html";
        }
    }
    

    另一方面,当您使用@RestController annotation 时,您总是返回 JSON 格式的响应文本,这就是为什么您总是得到相同的结果。

    【讨论】:

    • 即使我只映射一个,因为在我的控制器中只包含 /users/ 的映射,它不会映射到 localhost:8080/users/。如果我只有 /users/test,也一样。但是, /users 有效。方法名称不应该相同,这是我在问题中的一个错误。
    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 2019-11-25
    • 2022-01-12
    • 1970-01-01
    • 2020-08-03
    • 2020-11-08
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多