SpringBoot外部静态资源的访问

springboot项目访问jar外部静态资源,html举例子

简单描述,在启动类中实现WebMvcConfigurer接口,实现其addResourceHandlers方法即可。

@SpringBootApplication
@EnableSwagger2
@EnableSwaggerBootstrapUI
@MapperScan("com.job.mapper")//扫描mapper包
@PropertySource(value = "classpath:config/app.properties", encoding = "UTF-8")
public class Application implements CommandLineRunner, WebMvcConfigurer {

    private final Logger logger = LoggerFactory.getLogger(Application.class);

    @Value("${html.agreement}")
    // html.agreement=/agreement
    private String agreement;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("file:" + agreement + File.separator);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("Server started successfully...");
    }
}