Consistently list supported media types

Add constructors to HttpMediaTypeNotSupportedException and
UnsupportedMediaTypeStatusException for a parse error that also accept
the list of supported media types to include in the response headers.

Closes gh-28062
This commit is contained in:
rstoyanchev
2023-01-30 17:45:37 +00:00
parent e564a0de46
commit 9c6fd3ed06
8 changed files with 50 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -72,7 +72,8 @@ class DefaultServerRequest implements ServerRequest {
ex -> (ex.getContentType() != null ?
new UnsupportedMediaTypeStatusException(
ex.getContentType(), ex.getSupportedMediaTypes(), ex.getBodyType()) :
new UnsupportedMediaTypeStatusException(ex.getMessage()));
new UnsupportedMediaTypeStatusException(
ex.getMessage(), ex.getSupportedMediaTypes()));
private static final Function<DecodingException, ServerWebInputException> DECODING_MAPPER =
ex -> new ServerWebInputException("Failed to read HTTP message", null, ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -199,7 +199,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
contentType = request.getHeaders().getContentType();
}
catch (InvalidMediaTypeException ex) {
throw new UnsupportedMediaTypeStatusException(ex.getMessage());
throw new UnsupportedMediaTypeStatusException(ex.getMessage(), new ArrayList<>(mediaTypes));
}
throw new UnsupportedMediaTypeStatusException(
contentType, new ArrayList<>(mediaTypes), exchange.getRequest().getMethod());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -154,7 +154,8 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
}
catch (InvalidMediaTypeException ex) {
throw new UnsupportedMediaTypeStatusException(
"Can't parse Content-Type [" + headers.getFirst("Content-Type") + "]: " + ex.getMessage());
"Can't parse Content-Type [" + headers.getFirst("Content-Type") + "]: " + ex.getMessage(),
getSupportedMediaTypes(elementType));
}
MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);