首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Spring boot中,我似乎无法从URL中删除路径扩展名

在Spring Boot中,可以通过使用Spring MVC的路径扩展名配置来实现从URL中删除路径扩展名。

首先,确保你的Spring Boot项目中已经引入了Spring MVC依赖。在pom.xml文件中添加以下依赖:

代码语言:txt
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

接下来,在你的Spring Boot应用程序的配置类中,添加以下配置:

代码语言:txt
复制
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
    
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}

上述配置中,configurePathMatch方法用于禁用Spring MVC默认的URL路径解析规则,configureContentNegotiation方法用于禁用Spring MVC的内容协商机制,这样就可以实现从URL中删除路径扩展名。

现在,你可以在你的控制器中定义处理URL的方法,如下所示:

代码语言:txt
复制
@RestController
public class MyController {
    
    @GetMapping("/example")
    public String example() {
        return "Example";
    }
}

在上述示例中,@GetMapping("/example")注解定义了一个处理/example路径的GET请求的方法。当你访问/example时,将会返回字符串"Example"。

通过以上配置,你可以在Spring Boot中实现从URL中删除路径扩展名的功能。这样,当你访问/example.html时,也会调用example()方法并返回"Example"字符串。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券