如何向Spring Boot 2.3.1添加重写规则?

huangapple go评论36阅读模式
英文:

How to add rewrite rule to spring boot 2.3.1

问题

我有一个基于这个示例的Spring Boot应用程序。

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

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

我在这里找到了一些内容,但不幸的是对我不起作用,而且似乎org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory在Spring Boot 2.3.1中不再存在。

英文:

I have a Spring Boot application based on this example.

Now the question is how can I add rewrite rules to my application that add /index.html when user visit the root URL.

I mean when user visit http://localhost:8080/my-app or http://localhost:8080/my-app/ then I redirect him or her to http://localhost:8080/my-app/index.html.

I found something here, but unfortunately does not work for me, also it seems org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory does not exist in Spring Boot 2.3.1 anymore.

答案1

得分: 1

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

英文:

I only need to add a new controller, despite this application does not use MVC, this controller will redirect the / requests to /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";
    }
}

huangapple
  • 本文由 发表于 2020年7月31日 16:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定