我是 Spring Boot 新手,有一件事困扰着我:如果我有一个像这样的简单 Spring Boot 应用程序:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我可以使用 URL 从浏览器调用应用程序的默认网页:
http://localhost:8080/greeting
返回由 Controller 指定的名为 greeting.html
的页面:
@RequestMapping("/greeting")
public String greeting() Model model) {
return "greeting";
}
有没有办法让Spring Boot在浏览器中自动打开greeting.html
?我可以告诉 Spring boot 我希望它在项目启动时运行哪个 Controller 方法吗?
最佳答案
不确定是否可以针对这种情况进行设置,但您可以做的是使用重定向。
只需映射 "/"
并重定向到 "/greeting"
:
@RequestMapping("/")
public String index(Model model) {
return "redirect: greeting";
}
但是,如果您希望浏览器在运行应用程序后打开任何页面,您应该查找您的 IDE 是否可以做到这一点。
关于java - Spring Boot中自动调用应用程序的默认网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310165/