Add text/plain error response support

Refine `BasicErrorController` mappings so that only JSON and XML get
structured responses. A simple string response is returned for all
other media types.

Fixes gh-12513
This commit is contained in:
Phillip Webb 2018-03-16 14:30:42 -07:00
parent e975dbe3f0
commit 23892e33d6
2 changed files with 37 additions and 6 deletions

View File

@ -31,7 +31,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
@ -82,6 +81,16 @@ public class BasicErrorController extends AbstractErrorController {
return this.errorProperties.getPath();
}
@RequestMapping(produces = { "application/xml", "text/xml", "application/json",
"application/*+xml", "application/*+json" })
public ResponseEntity<Map<String, Object>> errorStructured(
HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
@ -94,12 +103,20 @@ public class BasicErrorController extends AbstractErrorController {
}
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
public ResponseEntity<String> errorText(HttpServletRequest request) {
Map<String, Object> attributes = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_PLAIN));
int padding = 0;
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
padding = Math.max(padding, entry.getKey().length());
}
StringBuffer body = new StringBuffer();
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
body.append(String.format("%-" + padding + "s : %s%n", entry.getKey(),
entry.getValue()));
}
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
return new ResponseEntity<String>(body.toString(), status);
}
/**

View File

@ -158,6 +158,7 @@ public class BasicErrorControllerIntegrationTests {
load();
RequestEntity request = RequestEntity
.post(URI.create(createUrl("/bodyValidation")))
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON).body("{}");
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
String resp = entity.getBody().toString();
@ -167,6 +168,19 @@ public class BasicErrorControllerIntegrationTests {
assertThat(resp).contains(MethodArgumentNotValidException.class.getName());
}
@Test
public void testRequestBodyValidationForText() throws Exception {
load();
RequestEntity<Void> request = RequestEntity.post(URI.create(createUrl("/")))
.accept(MediaType.TEXT_PLAIN).build();
ResponseEntity<String> entity = new TestRestTemplate().exchange(request,
String.class);
String resp = entity.getBody().toString();
assertThat(resp).contains("status");
assertThat(resp).contains("error");
assertThat(resp).contains(IllegalStateException.class.getName());
}
@Test
public void testConventionTemplateMapping() throws Exception {
load();