【问题标题】:How to add rewrite rule to spring boot 2.3.1如何将重写规则添加到 Spring Boot 2.3.1
【发布时间】:2020-11-21 02:28:32
【问题描述】:

我有一个基于 this example 的 Spring Boot 应用程序。

现在的问题是如何在我的应用程序中添加重写规则,当用户访问根 URL 时添加 /index.html

我的意思是当用户访问http://localhost:8080/my-apphttp://localhost:8080/my-app/ 时,我会将他或她重定向到http://localhost:8080/my-app/index.html

我找到了 here 的东西,但不幸的是对我不起作用,而且似乎 org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory 在 Spring Boot 2.3.1 中不再存在。

【问题讨论】:

标签: java spring-boot tomcat tomcat9


【解决方案1】:

我只需要添加一个新的控制器,尽管这个应用程序不使用MVC,这个控制器会将/请求重定向到/index.html

package me.cimply.ask.odata.controller;

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

@Controller
public class AppController {
    
    @GetMapping("/")
    public String index() {     
        return "redirect:index.html";
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2019-06-21
    相关资源
    最近更新 更多