Adapt to Spring MVC API updates

See gh-45487
This commit is contained in:
Stéphane Nicoll 2025-05-08 21:57:42 -07:00 committed by Phillip Webb
parent c15418b693
commit 05fb37d046
8 changed files with 3 additions and 165 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -60,14 +60,6 @@ class CompositeHandlerAdapter implements HandlerAdapter {
return null;
}
@Override
@Deprecated(since = "2.4.9", forRemoval = false)
@SuppressWarnings("deprecation")
public long getLastModified(HttpServletRequest request, Object handler) {
Optional<HandlerAdapter> adapter = getAdapter(handler);
return adapter.map((handlerAdapter) -> handlerAdapter.getLastModified(request, handler)).orElse(0L);
}
private Optional<HandlerAdapter> getAdapter(Object handler) {
if (this.adapters == null) {
this.adapters = extractAdapters();

View File

@ -249,11 +249,6 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
return null;
}
@Override
protected void extendInterceptors(List<Object> interceptors) {
interceptors.add(new SkipPathExtensionContentNegotiation());
}
/**
* Return the Handler providing actuator links at the root endpoint.
* @return the links handler

View File

@ -22,7 +22,6 @@ import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -163,9 +162,4 @@ public class ControllerEndpointHandlerMapping extends RequestMappingHandlerMappi
return this.corsConfiguration;
}
@Override
protected void extendInterceptors(List<Object> interceptors) {
interceptors.add(new SkipPathExtensionContentNegotiation());
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.endpoint.web.servlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
/**
* {@link HandlerInterceptor} to ensure that
* {@link org.springframework.web.accept.PathExtensionContentNegotiationStrategy} is
* skipped for web endpoints.
*
* @author Phillip Webb
*/
final class SkipPathExtensionContentNegotiation implements HandlerInterceptor {
@SuppressWarnings("deprecation")
private static final String SKIP_ATTRIBUTE = org.springframework.web.accept.PathExtensionContentNegotiationStrategy.class
.getName() + ".SKIP";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
request.setAttribute(SKIP_ATTRIBUTE, Boolean.TRUE);
return true;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -70,6 +70,7 @@ public class RequestMappingConditionsDescription {
.toList();
}
@SuppressWarnings("removal")
private Set<String> extractPathPatterns(RequestMappingInfo requestMapping) {
PatternsRequestCondition patternsCondition = requestMapping.getPatternsCondition();
return (patternsCondition != null) ? patternsCondition.getPatterns()

View File

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.web.servlet;
import java.time.Duration;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.Consumer;
@ -80,13 +79,9 @@ import org.springframework.util.CollectionUtils;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.filter.FormContentFilter;
@ -459,15 +454,6 @@ public class WebMvcAutoConfiguration {
return localeResolver;
}
@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.THEME_RESOLVER_BEAN_NAME)
@Deprecated(since = "3.0.0", forRemoval = false)
@SuppressWarnings("deprecation")
public org.springframework.web.servlet.ThemeResolver themeResolver() {
return super.themeResolver();
}
@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME)
@ -580,22 +566,6 @@ public class WebMvcAutoConfiguration {
}
}
@Bean
@Override
@SuppressWarnings("deprecation")
public ContentNegotiationManager mvcContentNegotiationManager() {
ContentNegotiationManager manager = super.mvcContentNegotiationManager();
List<ContentNegotiationStrategy> strategies = manager.getStrategies();
ListIterator<ContentNegotiationStrategy> iterator = strategies.listIterator();
while (iterator.hasNext()) {
ContentNegotiationStrategy strategy = iterator.next();
if (strategy instanceof org.springframework.web.accept.PathExtensionContentNegotiationStrategy) {
iterator.set(new OptionalPathExtensionContentNegotiationStrategy(strategy));
}
}
return manager;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
@ -683,33 +653,4 @@ public class WebMvcAutoConfiguration {
}
/**
* Decorator to make
* {@link org.springframework.web.accept.PathExtensionContentNegotiationStrategy}
* optional depending on a request attribute.
*/
static class OptionalPathExtensionContentNegotiationStrategy implements ContentNegotiationStrategy {
@SuppressWarnings("deprecation")
private static final String SKIP_ATTRIBUTE = org.springframework.web.accept.PathExtensionContentNegotiationStrategy.class
.getName() + ".SKIP";
private final ContentNegotiationStrategy delegate;
OptionalPathExtensionContentNegotiationStrategy(ContentNegotiationStrategy delegate) {
this.delegate = delegate;
}
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
throws HttpMediaTypeNotAcceptableException {
Object skip = webRequest.getAttribute(SKIP_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
if (skip != null && Boolean.parseBoolean(skip.toString())) {
return MEDIA_TYPE_ALL_LIST;
}
return this.delegate.resolveMediaTypes(webRequest);
}
}
}

View File

@ -158,15 +158,6 @@ class DispatcherServletAutoConfigurationTests {
});
}
@Test
@Deprecated(since = "3.2.0", forRemoval = true)
void dispatcherServletThrowExceptionIfNoHandlerFoundDefaultConfig() {
this.contextRunner.run((context) -> {
DispatcherServlet dispatcherServlet = context.getBean(DispatcherServlet.class);
assertThat(dispatcherServlet).extracting("throwExceptionIfNoHandlerFound").isEqualTo(true);
});
}
@Test
void dispatcherServletCustomConfig() {
this.contextRunner

View File

@ -110,7 +110,6 @@ import org.springframework.web.servlet.RequestToViewNameTranslator;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -865,16 +864,6 @@ class WebMvcAutoConfigurationTests {
});
}
@Test
void defaultContentNegotiation() {
this.contextRunner.run((context) -> {
RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager contentNegotiationManager = handlerMapping.getContentNegotiationManager();
assertThat(contentNegotiationManager.getStrategies()).doesNotHaveAnyElementsOfTypes(
WebMvcAutoConfiguration.OptionalPathExtensionContentNegotiationStrategy.class);
});
}
@Test
void queryParameterContentNegotiation() {
this.contextRunner.withPropertyValues("spring.mvc.contentnegotiation.favor-parameter:true").run((context) -> {
@ -885,16 +874,6 @@ class WebMvcAutoConfigurationTests {
});
}
@Test
void customConfigurerAppliedAfterAutoConfig() {
this.contextRunner.withUserConfiguration(CustomConfigurer.class).run((context) -> {
ContentNegotiationManager manager = context.getBean(ContentNegotiationManager.class);
assertThat(manager.getStrategies())
.anyMatch((strategy) -> WebMvcAutoConfiguration.OptionalPathExtensionContentNegotiationStrategy.class
.isAssignableFrom(strategy.getClass()));
});
}
@Test
void requestContextFilterIsAutoConfigured() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RequestContextFilter.class));
@ -1324,17 +1303,6 @@ class WebMvcAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class CustomConfigurer implements WebMvcConfigurer {
@Override
@SuppressWarnings("deprecation")
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true);
}
}
@Configuration(proxyBeanMethods = false)
static class CustomApplicationTaskExecutorConfig {