关于 WebRoot 配置

可以考虑将 webroot 目录放到 项目jar的同级

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
   

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        ApplicationHome home = new ApplicationHome(getClass());
        String jarParentPath = home.getSource().getParentFile().getAbsolutePath();

        String webrootPath = jarParentPath + "/webroot/";

        log.info("webrootPath:{}", webrootPath);
        registry.addResourceHandler("/**")
                .addResourceLocations("file:" + webrootPath);
    }

//设定 默认访问 index.html
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
}

POM 配置

需要将 webroot 在编译的时候 ,放到 target中 方便在调试的时候 可以访问到

 <!-- 其他插件配置保持不变 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>copy-webroot</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/webroot</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>./webroot</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容