1 Hello World
如何创建一个SpringBoot web项目我这里就不多赘述了,我们先看看如何发送一个Hello World 请求把:
- 创建一个HelloController
@RestController
public class HelloController {
@RequestMapping(method = RequestMethod.GET,path = "/hello")
public String hello(){
return "hello world";
}
}
这样访问http://localhost:8080/hello
就能返回给我们一个 hello world。
静态资源
我们知道现在创建的springboot项目是以打jar的包形式那如何加载静态资源呢?以前我们创建一个web项目我们可以把一些静态资源:css、js、jsp等等放在webapp目录下,那现在该如何做呢?
- WebMvcAutoConfiguration类
我们看看其中的addResourceHandlers这个方法:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache()
.getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(
this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
}
这个方法其实很简单先判断是否禁止我们访问默认的资源(好习惯,大家写代码的时候也尽量这样写),然后注册我们的静态资源在/webjars/**
目录下,这就意味着我们的静态资源可以放在该目录下classpath:/META-INF/resources/webjars/
。最后我们可以设置和静态资源有关的参数,缓存时间等。
这里呢如何让我们想引入jquery、js等相关的包可以直接添加在pom文件中可以在https://www.webjars.org/
这个网站中区查找。
- 实例
<!--jquery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>
引入pom文件后我们可以发现:
我们可以通过 localhost:8080/webjars/jquery/3.3.1/jquery.js
去访问;所以说我们以后可以直接引入依赖加入到pom文件中即可。
那如果引入我们自己写的给该怎么办呢?其实在刚刚上面的一段代码中
静态资源文件夹映射
//静态资源文件夹映射
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(
this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
我们可以点进这个方法中看看this.resourceProperties.getStaticLocations()
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
/**
* Whether to enable default resource handling.
*/
private boolean addMappings = true;
“/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
- 静态资源文件夹
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
那么我们访问localhost:8080/abc
===> 去静态资源文件夹里面找abc
配置欢迎页映射
WelcomePageHandlerMapping类可以设置欢迎页面映射回去找index页面
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
我们可以在静态资源目录下配置index页面,如歌没有没有配置访问localhost://8080
会显示404
配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}
所有的 **/xxx.ico 都是在静态资源文件下找;
下图就是静态资源文件夹可以是public、resoueces、static任选一个都可以:
这些静态资源的加载位置都是springboot默认给我们创建的,当然我们可以自己设置可以通过:
spring.resources.static-locations=classpath:/hello,classpath:/spring
配置多个用逗号分隔。