From 95d7f883ae1cb3ad4554eb37c54e81d524015880 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 13 Jul 2021 12:11:20 +0100 Subject: [PATCH] Deprecate LastModified See gh-27075 --- .../springframework/web/servlet/DispatcherServlet.java | 1 + .../springframework/web/servlet/HandlerAdapter.java | 7 ++++--- .../function/support/HandlerFunctionAdapter.java | 3 ++- .../servlet/handler/SimpleServletHandlerAdapter.java | 3 ++- .../springframework/web/servlet/mvc/Controller.java | 7 ++++--- .../web/servlet/mvc/HttpRequestHandlerAdapter.java | 4 ++-- .../springframework/web/servlet/mvc/LastModified.java | 8 +++++++- .../servlet/mvc/SimpleControllerHandlerAdapter.java | 4 ++-- .../mvc/method/AbstractHandlerMethodAdapter.java | 6 +++++- .../annotation/RequestMappingHandlerAdapter.java | 1 + .../web/servlet/SimpleWebApplicationContext.java | 6 +++--- src/docs/asciidoc/web/webmvc.adoc | 10 +++------- 12 files changed, 36 insertions(+), 24 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index 1486837d7f92..19cb32564ce7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -1020,6 +1020,7 @@ private void logRequest(HttpServletRequest request) { * @param response current HTTP response * @throws Exception in case of any kind of processing failure */ + @SuppressWarnings("deprecation") protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerAdapter.java index ade41e5149a8..cd43bdce5b15 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -83,9 +83,10 @@ public interface HandlerAdapter { * @param request current HTTP request * @param handler the handler to use * @return the lastModified value for the given handler - * @see javax.servlet.http.HttpServlet#getLastModified - * @see org.springframework.web.servlet.mvc.LastModified#getLastModified + * @deprecated as of 5.3.9 along with + * {@link org.springframework.web.servlet.mvc.LastModified}. */ + @Deprecated long getLastModified(HttpServletRequest request, Object handler); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/HandlerFunctionAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/HandlerFunctionAdapter.java index 20f206aa3e3f..67fac910b781 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/HandlerFunctionAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/HandlerFunctionAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -157,6 +157,7 @@ else if (result == null) { } @Override + @SuppressWarnings("deprecation") public long getLastModified(HttpServletRequest request, Object handler) { return -1L; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleServletHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleServletHandlerAdapter.java index f6881ed61b22..7cc9fcf9f0d3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleServletHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleServletHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2021 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. @@ -69,6 +69,7 @@ public ModelAndView handle(HttpServletRequest request, HttpServletResponse respo } @Override + @SuppressWarnings("deprecation") public long getLastModified(HttpServletRequest request, Object handler) { return -1; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/Controller.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/Controller.java index 4c9f45fddd15..4f59c52ca1b5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/Controller.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/Controller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 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. @@ -93,11 +93,12 @@ * you all those references through convenient accessors but requires an * ApplicationContext reference on initialization. * - *

Controllers can optionally implement the {@link LastModified} interface. + *

Controllers can use the {@code checkNotModified} methods on + * {@link org.springframework.web.context.request.WebRequest} for HTTP caching + * support. * * @author Rod Johnson * @author Juergen Hoeller - * @see LastModified * @see SimpleControllerHandlerAdapter * @see AbstractController * @see org.springframework.mock.web.MockHttpServletRequest diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/HttpRequestHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/HttpRequestHandlerAdapter.java index 1b10d58c3be1..161750bc55dc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/HttpRequestHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/HttpRequestHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2021 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. @@ -35,7 +35,6 @@ * @since 2.0 * @see org.springframework.web.servlet.DispatcherServlet * @see org.springframework.web.HttpRequestHandler - * @see LastModified * @see SimpleControllerHandlerAdapter */ public class HttpRequestHandlerAdapter implements HandlerAdapter { @@ -55,6 +54,7 @@ public ModelAndView handle(HttpServletRequest request, HttpServletResponse respo } @Override + @SuppressWarnings("deprecation") public long getLastModified(HttpServletRequest request, Object handler) { if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/LastModified.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/LastModified.java index 55abd1915e48..a6cfdcf86a69 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/LastModified.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/LastModified.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2021 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. @@ -34,12 +34,18 @@ * * @author Rod Johnson * @author Juergen Hoeller + * @deprecated as of 5.3.9 in favor of using the {@code checkNotModified} methods + * in {@link org.springframework.web.context.request.WebRequest}, or from an + * annotated controller method, returning a + * {@link org.springframework.http.ResponseEntity} with an "ETag" and/or + * "Last-Modified" headers set. * @see javax.servlet.http.HttpServlet#getLastModified * @see Controller * @see SimpleControllerHandlerAdapter * @see org.springframework.web.HttpRequestHandler * @see HttpRequestHandlerAdapter */ +@Deprecated public interface LastModified { /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/SimpleControllerHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/SimpleControllerHandlerAdapter.java index 62d249ce80d0..64372c665c60 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/SimpleControllerHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/SimpleControllerHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2021 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. @@ -34,7 +34,6 @@ * @author Juergen Hoeller * @see org.springframework.web.servlet.DispatcherServlet * @see Controller - * @see LastModified * @see HttpRequestHandlerAdapter */ public class SimpleControllerHandlerAdapter implements HandlerAdapter { @@ -53,6 +52,7 @@ public ModelAndView handle(HttpServletRequest request, HttpServletResponse respo } @Override + @SuppressWarnings("deprecation") public long getLastModified(HttpServletRequest request, Object handler) { if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java index c8219f02be72..24ef3cb6573e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -105,6 +105,7 @@ protected abstract ModelAndView handleInternal(HttpServletRequest request, * This implementation expects the handler to be an {@link HandlerMethod}. */ @Override + @SuppressWarnings("deprecation") public final long getLastModified(HttpServletRequest request, Object handler) { return getLastModifiedInternal(request, (HandlerMethod) handler); } @@ -114,7 +115,10 @@ public final long getLastModified(HttpServletRequest request, Object handler) { * @param request current HTTP request * @param handlerMethod handler method to use * @return the lastModified value for the given handler + * @deprecated as of 5.3.9 along with + * {@link org.springframework.web.servlet.mvc.LastModified}. */ + @Deprecated protected abstract long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 9e5ea44432ec..6005b22e247b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -826,6 +826,7 @@ protected ModelAndView handleInternal(HttpServletRequest request, * and return {@code null} if the result of that call is {@code true}. */ @Override + @SuppressWarnings("deprecation") protected long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod) { return -1; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java index 6c2e1ed2c0da..b6579b960ba4 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -34,7 +34,6 @@ import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; import org.springframework.web.servlet.mvc.Controller; -import org.springframework.web.servlet.mvc.LastModified; import org.springframework.web.servlet.support.RequestContextUtils; import org.springframework.web.servlet.theme.AbstractThemeResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; @@ -68,7 +67,8 @@ public void refresh() throws BeansException { } - public static class LocaleChecker implements Controller, LastModified { + @SuppressWarnings("deprecation") + public static class LocaleChecker implements Controller, org.springframework.web.servlet.mvc.LastModified { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) diff --git a/src/docs/asciidoc/web/webmvc.adoc b/src/docs/asciidoc/web/webmvc.adoc index 01bc7d18329b..2133ce377f86 100644 --- a/src/docs/asciidoc/web/webmvc.adoc +++ b/src/docs/asciidoc/web/webmvc.adoc @@ -527,13 +527,9 @@ The `HandlerExceptionResolver` beans declared in the `WebApplicationContext` are resolve exceptions thrown during request processing. Those exception resolvers allow customizing the logic to address exceptions. See <> for more details. -The Spring `DispatcherServlet` also supports the return of the -`last-modification-date`, as specified by the Servlet API. The process of determining -the last modification date for a specific request is straightforward: The -`DispatcherServlet` looks up an appropriate handler mapping and tests whether the -handler that is found implements the `LastModified` interface. If so, the value of the -`long getLastModified(request)` method of the `LastModified` interface is returned to -the client. +For HTTP caching support, handlers can use the `checkNotModified` methods of `WebRequest`, +along with further options for annoated controllers as described in +<>. You can customize individual `DispatcherServlet` instances by adding Servlet initialization parameters (`init-param` elements) to the Servlet declaration in the