Add checkNotModified support for view resolution
Issue: SPR-14522
This commit is contained in:
@@ -230,6 +230,9 @@ public class ViewResolutionResultHandler extends ContentNegotiatingResultHandler
|
||||
}
|
||||
|
||||
private Mono<Object> getDefaultViewNameMono(ServerWebExchange exchange, HandlerResult result) {
|
||||
if (exchange.isNotModified()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
String defaultViewName = getDefaultViewName(result, exchange);
|
||||
if (defaultViewName != null) {
|
||||
return Mono.just(defaultViewName);
|
||||
|
||||
@@ -56,6 +56,10 @@ public abstract class AbstractRequestMappingIntegrationTests extends AbstractHtt
|
||||
return this.applicationContext;
|
||||
}
|
||||
|
||||
RestTemplate getRestTemplate() {
|
||||
return this.restTemplate;
|
||||
}
|
||||
|
||||
|
||||
<T> ResponseEntity<T> performGet(String url, MediaType out,
|
||||
Class<T> type) throws Exception {
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.reactive.result.method.annotation;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -23,7 +26,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -31,8 +37,11 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.reactive.config.ViewResolverRegistry;
|
||||
import org.springframework.web.reactive.config.WebReactiveConfiguration;
|
||||
import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.springframework.http.RequestEntity.get;
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,6 +67,16 @@ public class RequestMappingViewResolutionIntegrationTests extends AbstractReques
|
||||
assertEquals(expected, performGet("/html?name=Jason", MediaType.TEXT_HTML, String.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void etagCheckWithNotModifiedResponse() throws Exception {
|
||||
URI uri = new URI("http://localhost:" + this.port + "/html");
|
||||
RequestEntity<Void> request = get(uri).ifNoneMatch("\"deadb33f8badf00d\"").build();
|
||||
ResponseEntity<String> response = getRestTemplate().exchange(request, String.class);
|
||||
|
||||
assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
|
||||
assertNull(response.getBody());
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(resourcePattern = "**/RequestMappingViewResolutionIntegrationTests$*.class")
|
||||
@@ -84,10 +103,16 @@ public class RequestMappingViewResolutionIntegrationTests extends AbstractReques
|
||||
private static class TestController {
|
||||
|
||||
@GetMapping("/html")
|
||||
public String getHtmlPage(@RequestParam String name, Model model) {
|
||||
model.addAttribute("hello", "Hello: " + name + "!");
|
||||
public String getHtmlPage(@RequestParam Optional<String> name, Model model,
|
||||
ServerWebExchange exchange) {
|
||||
|
||||
if (exchange.checkNotModified("deadb33f8badf00d")) {
|
||||
return null;
|
||||
}
|
||||
model.addAttribute("hello", "Hello: " + name.orElse("<no name>") + "!");
|
||||
return "test";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -68,6 +68,12 @@ public interface ServerWebExchange {
|
||||
*/
|
||||
Mono<WebSession> getSession();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the one of the {@code checkNotModified} methods
|
||||
* in this contract were used and they returned true.
|
||||
*/
|
||||
boolean isNotModified();
|
||||
|
||||
/**
|
||||
* An overloaded variant of {@link #checkNotModified(String, Instant)} with
|
||||
* a last-modified timestamp only.
|
||||
|
||||
@@ -104,6 +104,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
return this.sessionMono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNotModified() {
|
||||
return this.notModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNotModified(Instant lastModified) {
|
||||
return checkNotModified(null, lastModified);
|
||||
|
||||
Reference in New Issue
Block a user