前言
当使用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);