spring Boot access static resources

First, the default static resource mapping

Spring Boot static resource mapping provides a default configuration

Spring Boot by default / ** All access maps to the following directory:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

Each browser access:

http://localhost:8080/a.jpg
http://localhost:8080/b.jpg
http://localhost:8080/c.jpg

Pictures can normally access the appropriate resources. So explain, Spring Boot default from public resources static one by one to find whether there exist appropriate resources, if there is a direct return.

Custom static resource mapping

In the actual development, you may need a custom static resource access path, you can inherit WebMvcConfigurerAdapter to achieve.

The first way: static resource allocation class

package com.sam.demo.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 配置静态资源映射
 * @author sam
 * @since 2017/7/16
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

Restart the project, visit: http: // localhost: 8080 / static / c.jpg picture c.jpg resources under normal access to static directory. 

 

Guess you like

Origin blog.csdn.net/anwarkanji/article/details/90582406