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.
@@ -54,7 +54,17 @@ public class HttpMediaTypeNotSupportedException extends HttpMediaTypeException {
* @param message the exception message
*/
public HttpMediaTypeNotSupportedException(String message) {
super(message, Collections.emptyList(), PARSE_ERROR_DETAIL_CODE, null);
this(message, Collections.emptyList());
}
/**
* Create a new HttpMediaTypeNotSupportedException for a parse error.
* @param message the exception message
* @param mediaTypes list of supported media types
* @since 6.0.5
*/
public HttpMediaTypeNotSupportedException(String message, List<MediaType> mediaTypes) {
super(message, mediaTypes, PARSE_ERROR_DETAIL_CODE, null);
this.contentType = null;
this.httpMethod = null;
getBody().setDetail("Could not parse Content-Type.");

View File

@@ -57,9 +57,17 @@ public class UnsupportedMediaTypeStatusException extends ResponseStatusException
* Constructor for when the specified Content-Type is invalid.
*/
public UnsupportedMediaTypeStatusException(@Nullable String reason) {
this(reason, Collections.emptyList());
}
/**
* Constructor for when the specified Content-Type is invalid.
* @since 6.0.5
*/
public UnsupportedMediaTypeStatusException(@Nullable String reason, List<MediaType> supportedTypes) {
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, reason, null, PARSE_ERROR_DETAIL_CODE, null);
this.contentType = null;
this.supportedMediaTypes = Collections.emptyList();
this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes);
this.bodyType = null;
this.method = null;
setDetail("Could not parse Content-Type.");