Browse Source

Add convention based status error pages

Update `spring-boot-sample-web-mustache` to show how convention based
status error pages can be used.

See gh-2691
pull/5815/merge
Phillip Webb 10 years ago
parent
commit
6d069de79f
  1. 29
      spring-boot-samples/spring-boot-sample-web-mustache/src/main/java/sample/mustache/WelcomeController.java
  2. 9
      spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/public/error/503.html
  3. 9
      spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/public/error/5xx.html
  4. 9
      spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/templates/error/507.html
  5. 38
      spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java

29
spring-boot-samples/spring-boot-sample-web-mustache/src/main/java/sample/mustache/WelcomeController.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -20,8 +20,10 @@ import java.util.Date; @@ -20,8 +20,10 @@ import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
@Controller
public class WelcomeController {
@ -36,4 +38,29 @@ public class WelcomeController { @@ -36,4 +38,29 @@ public class WelcomeController {
return "welcome";
}
@RequestMapping("/serviceUnavailable")
public String ServiceUnavailable() {
throw new ServiceUnavailableException();
}
@RequestMapping("/bang")
public String bang() {
throw new RuntimeException("Boom");
}
@RequestMapping("/insufficientStorage")
public String insufficientStorage() {
throw new InsufficientStorageException();
}
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
private static class ServiceUnavailableException extends RuntimeException {
}
@ResponseStatus(HttpStatus.INSUFFICIENT_STORAGE)
private static class InsufficientStorageException extends RuntimeException {
}
}

9
spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/public/error/503.html

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<body>
I'm a 503
</body>
</html>

9
spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/public/error/5xx.html

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<body>
I'm a 5xx
</body>
</html>

9
spring-boot-samples/spring-boot-sample-web-mustache/src/main/resources/templates/error/507.html

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<body>
I'm a 507
</body>
</html>

38
spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java

@ -63,14 +63,48 @@ public class SampleWebMustacheApplicationTests { @@ -63,14 +63,48 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> responseEntity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET,
requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(responseEntity.getBody())
.contains("Something went wrong: 404 Not Found");
}
@Test
public void test503HtmlResource() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/serviceUnavailable", HttpMethod.GET,
requestEntity, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
assertThat(entity.getBody()).contains("I'm a 503");
}
@Test
public void test5xxHtmlResource() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/bang", HttpMethod.GET, requestEntity,
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody()).contains("I'm a 5xx");
}
@Test
public void test507Template() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/insufficientStorage", HttpMethod.GET,
requestEntity, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INSUFFICIENT_STORAGE);
assertThat(entity.getBody()).contains("I'm a 507");
}
}

Loading…
Cancel
Save