1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .util .List ;
20
20
21
+ import org .springframework .http .HttpHeaders ;
22
+ import org .springframework .http .HttpMethod ;
23
+ import org .springframework .http .HttpStatus ;
21
24
import org .springframework .http .MediaType ;
22
25
import org .springframework .lang .Nullable ;
26
+ import org .springframework .util .CollectionUtils ;
23
27
24
28
/**
25
29
* Exception thrown when a client POSTs, PUTs, or PATCHes content of a type
26
30
* not supported by request handler.
27
31
*
28
32
* @author Arjen Poutsma
33
+ * @author Rossen Stoyanchev
29
34
* @since 3.0
30
35
*/
31
36
@ SuppressWarnings ("serial" )
@@ -34,6 +39,9 @@ public class HttpMediaTypeNotSupportedException extends HttpMediaTypeException {
34
39
@ Nullable
35
40
private final MediaType contentType ;
36
41
42
+ @ Nullable
43
+ private final HttpMethod httpMethod ;
44
+
37
45
38
46
/**
39
47
* Create a new HttpMediaTypeNotSupportedException.
@@ -42,6 +50,8 @@ public class HttpMediaTypeNotSupportedException extends HttpMediaTypeException {
42
50
public HttpMediaTypeNotSupportedException (String message ) {
43
51
super (message );
44
52
this .contentType = null ;
53
+ this .httpMethod = null ;
54
+ getBody ().setDetail ("Could not parse Content-Type" );
45
55
}
46
56
47
57
/**
@@ -50,21 +60,38 @@ public HttpMediaTypeNotSupportedException(String message) {
50
60
* @param supportedMediaTypes the list of supported media types
51
61
*/
52
62
public HttpMediaTypeNotSupportedException (@ Nullable MediaType contentType , List <MediaType > supportedMediaTypes ) {
53
- this (contentType , supportedMediaTypes , "Content type '" +
54
- (contentType != null ? contentType : "" ) + "' not supported" );
63
+ this (contentType , supportedMediaTypes , null );
55
64
}
56
65
57
66
/**
58
67
* Create a new HttpMediaTypeNotSupportedException.
59
68
* @param contentType the unsupported content type
60
69
* @param supportedMediaTypes the list of supported media types
61
- * @param msg the detail message
70
+ * @param httpMethod the HTTP method of the request
71
+ * @since 6.0
62
72
*/
63
73
public HttpMediaTypeNotSupportedException (@ Nullable MediaType contentType ,
64
- List <MediaType > supportedMediaTypes , String msg ) {
74
+ List <MediaType > supportedMediaTypes , @ Nullable HttpMethod httpMethod ) {
75
+
76
+ this (contentType , supportedMediaTypes , httpMethod ,
77
+ "Content-Type " + (contentType != null ? "'" + contentType + "' " : "" ) + "is not supported" );
78
+ }
65
79
66
- super (msg , supportedMediaTypes );
80
+ /**
81
+ * Create a new HttpMediaTypeNotSupportedException.
82
+ * @param contentType the unsupported content type
83
+ * @param supportedMediaTypes the list of supported media types
84
+ * @param httpMethod the HTTP method of the request
85
+ * @param message the detail message
86
+ * @since 6.0
87
+ */
88
+ public HttpMediaTypeNotSupportedException (@ Nullable MediaType contentType ,
89
+ List <MediaType > supportedMediaTypes , @ Nullable HttpMethod httpMethod , String message ) {
90
+
91
+ super (message , supportedMediaTypes );
67
92
this .contentType = contentType ;
93
+ this .httpMethod = httpMethod ;
94
+ getBody ().setDetail ("Content-Type " + this .contentType + " is not supported" );
68
95
}
69
96
70
97
@@ -76,4 +103,22 @@ public MediaType getContentType() {
76
103
return this .contentType ;
77
104
}
78
105
106
+ @ Override
107
+ public int getRawStatusCode () {
108
+ return HttpStatus .UNSUPPORTED_MEDIA_TYPE .value ();
109
+ }
110
+
111
+ @ Override
112
+ public HttpHeaders getHeaders () {
113
+ if (CollectionUtils .isEmpty (getSupportedMediaTypes ())) {
114
+ return HttpHeaders .EMPTY ;
115
+ }
116
+ HttpHeaders headers = new HttpHeaders ();
117
+ headers .setAccept (getSupportedMediaTypes ());
118
+ if (HttpMethod .PATCH .equals (this .httpMethod )) {
119
+ headers .setAcceptPatch (getSupportedMediaTypes ());
120
+ }
121
+ return headers ;
122
+ }
123
+
79
124
}
0 commit comments