Browse Source

Fix HTTP status error template rendering in WebFlux

Prior to this commit, a change in `HttpStatus.toString` since SPR-16898
prevented the default WebFlux `ErrorWebExceptionHandler` to render
template views for exact HTTP status (e.g. "404.html").
This issue does not affect the resolution of series, like "4xx.html".

This commit fixes `DefaultErrorWebExceptionHandler` to use
`HttpStatus.value()` when attempting to resolve error views.

Closes gh-15083
pull/15084/head
Brian Clozel 7 years ago
parent
commit
da53a0b8d5
  1. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java
  2. 37
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java
  3. 1
      spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/web/reactive/error/templates/error/404.mustache
  4. 1
      spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/web/reactive/error/templates/error/4xx.mustache

2
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java

@ -122,7 +122,7 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa @@ -122,7 +122,7 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
ServerResponse.BodyBuilder responseBody = ServerResponse.status(errorStatus)
.contentType(MediaType.TEXT_HTML);
return Flux
.just("error/" + errorStatus.toString(),
.just("error/" + errorStatus.value(),
"error/" + SERIES_VIEWS.get(errorStatus.series()), "error/error")
.flatMap((viewName) -> renderErrorView(viewName, responseBody, error))
.switchIfEmpty(this.errorProperties.getWhitelabel().isEnabled()

37
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java

@ -257,6 +257,43 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { @@ -257,6 +257,43 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
});
}
@Test
public void exactStatusTemplateErrorPage() {
this.contextRunner
.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=" + getErrorTemplatesLocation())
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/notfound")
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isNotFound().expectBody(String.class).returnResult()
.getResponseBody();
assertThat(body).contains("404 page");
});
}
@Test
public void seriesStatusTemplateErrorPage() {
this.contextRunner
.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=" + getErrorTemplatesLocation())
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/badRequest")
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isBadRequest().expectBody(String.class).returnResult()
.getResponseBody();
assertThat(body).contains("4xx page");
});
}
private String getErrorTemplatesLocation() {
String packageName = getClass().getPackage().getName();
return "classpath:/" + packageName.replace('.', '/') + "/templates/";
}
@Test
public void invalidAcceptMediaType() {
this.contextRunner.run((context) -> {

1
spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/web/reactive/error/templates/error/404.mustache

@ -0,0 +1 @@ @@ -0,0 +1 @@
404 page

1
spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/web/reactive/error/templates/error/4xx.mustache

@ -0,0 +1 @@ @@ -0,0 +1 @@
4xx page
Loading…
Cancel
Save