ErrorResponse support in Spring MVC exception hierarchy

All Spring MVC exceptions from spring-web, now implement ErrorResponse
and expose HTTP error response information, including an RFC 7807 body.

See gh-27052
This commit is contained in:
rstoyanchev
2022-02-25 11:38:00 +00:00
parent 3efedef161
commit 76be6373a8
22 changed files with 230 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2022 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.
@@ -21,10 +21,13 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
/**
* By default when the DispatcherServlet can't find a handler for a request it
* sends a 404 response. However if its property "throwExceptionIfNoHandlerFound"
* By default, when the DispatcherServlet can't find a handler for a request it
* sends a 404 response. However, if its property "throwExceptionIfNoHandlerFound"
* is set to {@code true} this exception is raised and may be handled with
* a configured HandlerExceptionResolver.
*
@@ -34,7 +37,7 @@ import org.springframework.http.HttpHeaders;
* @see DispatcherServlet#noHandlerFound(HttpServletRequest, HttpServletResponse)
*/
@SuppressWarnings("serial")
public class NoHandlerFoundException extends ServletException {
public class NoHandlerFoundException extends ServletException implements ErrorResponse {
private final String httpMethod;
@@ -42,6 +45,8 @@ public class NoHandlerFoundException extends ServletException {
private final HttpHeaders headers;
private final ProblemDetail detail = ProblemDetail.forRawStatusCode(getRawStatusCode());
/**
* Constructor for NoHandlerFoundException.
@@ -57,6 +62,11 @@ public class NoHandlerFoundException extends ServletException {
}
@Override
public int getRawStatusCode() {
return HttpStatus.NOT_FOUND.value();
}
public String getHttpMethod() {
return this.httpMethod;
}
@@ -69,4 +79,9 @@ public class NoHandlerFoundException extends ServletException {
return this.headers;
}
@Override
public ProblemDetail getBody() {
return this.detail;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -206,7 +206,7 @@ class DefaultServerRequest implements ServerRequest {
return theConverter.read(clazz, this.serverHttpRequest);
}
}
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(bodyClass));
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(bodyClass), method());
}
private List<MediaType> getSupportedMediaTypes(Class<?> bodyClass) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@@ -313,7 +313,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
return theConverter.read(clazz, inputMessage);
}
}
throw new HttpMediaTypeNotSupportedException(contentType, Collections.emptyList());
throw new HttpMediaTypeNotSupportedException(contentType, Collections.emptyList(), method());
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -264,7 +264,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}
}
throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes));
throw new HttpMediaTypeNotSupportedException(
contentType, new ArrayList<>(mediaTypes), HttpMethod.valueOf(request.getMethod()));
}
if (helper.hasProducesMismatch()) {

View File

@@ -206,7 +206,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
return null;
}
throw new HttpMediaTypeNotSupportedException(contentType,
getSupportedMediaTypes(targetClass != null ? targetClass : Object.class));
getSupportedMediaTypes(targetClass != null ? targetClass : Object.class), httpMethod);
}
MediaType selectedContentType = contentType;