Commit f9e3e0d5 authored by Phillip Webb's avatar Phillip Webb

Register default resource path using a Resource

Update `WebMvcAutoConfiguration` so that the default "/" resource path
is registered directly as a `ServletContextResource`.

Closes gh-24745
parent 46629ef5
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.web.servlet; ...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.web.servlet;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -49,9 +48,9 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping { ...@@ -49,9 +48,9 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
private static final List<MediaType> MEDIA_TYPES_ALL = Collections.singletonList(MediaType.ALL); private static final List<MediaType> MEDIA_TYPES_ALL = Collections.singletonList(MediaType.ALL);
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) { ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) { if (welcomePage != null && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get()); logger.info("Adding welcome page: " + welcomePage);
setRootViewName("forward:index.html"); setRootViewName("forward:index.html");
} }
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) { else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
......
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -814,7 +814,7 @@ class WebMvcAutoConfigurationTests { ...@@ -814,7 +814,7 @@ class WebMvcAutoConfigurationTests {
protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) { protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) {
Object bean = context.getBean("resourceHandlerMapping"); Object bean = context.getBean("resourceHandlerMapping");
if (bean instanceof HandlerMapping) { if (bean instanceof HandlerMapping) {
return getMappingLocations(context.getBean("resourceHandlerMapping", HandlerMapping.class)); return getMappingLocations((HandlerMapping) bean);
} }
assertThat(bean.toString()).isEqualTo("null"); assertThat(bean.toString()).isEqualTo("null");
return Collections.emptyMap(); return Collections.emptyMap();
......
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.web.servlet; ...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.web.servlet;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -162,8 +161,7 @@ class WelcomePageHandlerMappingTests { ...@@ -162,8 +161,7 @@ class WelcomePageHandlerMappingTests {
return new WelcomePageHandlerMapping( return new WelcomePageHandlerMapping(
templateAvailabilityProviders templateAvailabilityProviders
.getIfAvailable(() -> new TemplateAvailabilityProviders(applicationContext)), .getIfAvailable(() -> new TemplateAvailabilityProviders(applicationContext)),
applicationContext, Optional.ofNullable(staticIndexPage.getIfAvailable()), staticPathPattern); applicationContext, staticIndexPage.getIfAvailable(), staticPathPattern);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment