Springboot如何实现设置默认访问路径的方法?

2026-04-30 03:262阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计494个文字,预计阅读时间需要2分钟。

Springboot如何实现设置默认访问路径的方法?

前言:在使用Spring Boot及其它框架结合编写Web前后端时,可能存在以下需求:

我想要在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面。(端口已省略,自行配置)

解决+方案1:实现重定向功能。

代码示例:java@Controllerpublic class RedirectController {

@GetMapping(/) public String redirectToHello() { return redirect:/hello; }}

前言

当使用springboot与其他框架结合编写web前后端时,可能存在这样的需求:我想在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面。(端口已省略,自行设置)

解决

方案1 - 实现WebMvcConfigurer接口

搜过很多博客,里面的内容虽然可以用。但是基本上都是基于继承WebMvcConfigurerAdapter类实现的,而官方的源码里面已经不推荐使用该类了。

下面给出我的解决方案,很简单:

import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class DefaultView implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("hello"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }

补充&注意点

setViewName :将 / 指向 /hello

这里需要注意,因为我的项目里把url的访问路径后缀".html"全部都去掉了,所以可以这么用。如果你的不是,需要做对应调整。

补充我的application.properties文件部分配置:

server.port=80 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.mvc.favicon.enabled=true

@Configuration 别忘了加,我自己就是忘了加,以为没生效,折腾半天。

方案2 - @Controller路由设置

@Controller public class PageController { @GetMapping(value = "/") public String defaultPath(Model model) { return "hello"; } }

properties文件配置同方案1一致

到此这篇关于Springboot设置默认访问路径方法实现的文章就介绍到这了,更多相关Springboot 默认访问路径内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

Springboot如何实现设置默认访问路径的方法?

本文共计494个文字,预计阅读时间需要2分钟。

Springboot如何实现设置默认访问路径的方法?

前言:在使用Spring Boot及其它框架结合编写Web前后端时,可能存在以下需求:

我想要在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面。(端口已省略,自行配置)

解决+方案1:实现重定向功能。

代码示例:java@Controllerpublic class RedirectController {

@GetMapping(/) public String redirectToHello() { return redirect:/hello; }}

前言

当使用springboot与其他框架结合编写web前后端时,可能存在这样的需求:我想在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面。(端口已省略,自行设置)

解决

方案1 - 实现WebMvcConfigurer接口

搜过很多博客,里面的内容虽然可以用。但是基本上都是基于继承WebMvcConfigurerAdapter类实现的,而官方的源码里面已经不推荐使用该类了。

下面给出我的解决方案,很简单:

import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class DefaultView implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("hello"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }

补充&注意点

setViewName :将 / 指向 /hello

这里需要注意,因为我的项目里把url的访问路径后缀".html"全部都去掉了,所以可以这么用。如果你的不是,需要做对应调整。

补充我的application.properties文件部分配置:

server.port=80 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.mvc.favicon.enabled=true

@Configuration 别忘了加,我自己就是忘了加,以为没生效,折腾半天。

方案2 - @Controller路由设置

@Controller public class PageController { @GetMapping(value = "/") public String defaultPath(Model model) { return "hello"; } }

properties文件配置同方案1一致

到此这篇关于Springboot设置默认访问路径方法实现的文章就介绍到这了,更多相关Springboot 默认访问路径内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

Springboot如何实现设置默认访问路径的方法?