16
16
17
17
package org .springframework .boot .autoconfigure .web .servlet .error ;
18
18
19
+ import java .net .URI ;
19
20
import java .util .Collections ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
24
25
import jakarta .servlet .http .HttpServletResponse ;
25
26
26
27
import org .springframework .boot .autoconfigure .web .ErrorProperties ;
28
+ import org .springframework .boot .autoconfigure .web .servlet .WebMvcProperties ;
27
29
import org .springframework .boot .web .error .ErrorAttributeOptions ;
28
30
import org .springframework .boot .web .error .ErrorAttributeOptions .Include ;
29
31
import org .springframework .boot .web .servlet .error .ErrorAttributes ;
30
32
import org .springframework .boot .web .servlet .server .AbstractServletWebServerFactory ;
31
33
import org .springframework .http .HttpStatus ;
32
34
import org .springframework .http .MediaType ;
35
+ import org .springframework .http .ProblemDetail ;
33
36
import org .springframework .http .ResponseEntity ;
34
37
import org .springframework .stereotype .Controller ;
35
38
import org .springframework .util .Assert ;
49
52
* @author Michael Stummvoll
50
53
* @author Stephane Nicoll
51
54
* @author Scott Frederick
55
+ * @author Yanming Zhou
52
56
* @since 1.0.0
53
57
* @see ErrorAttributes
54
58
* @see ErrorProperties
@@ -59,26 +63,33 @@ public class BasicErrorController extends AbstractErrorController {
59
63
60
64
private final ErrorProperties errorProperties ;
61
65
66
+ private final WebMvcProperties webMvcProperties ;
67
+
62
68
/**
63
69
* Create a new {@link BasicErrorController} instance.
64
70
* @param errorAttributes the error attributes
65
71
* @param errorProperties configuration properties
72
+ * @param webMvcProperties webMvc properties
66
73
*/
67
- public BasicErrorController (ErrorAttributes errorAttributes , ErrorProperties errorProperties ) {
68
- this (errorAttributes , errorProperties , Collections .emptyList ());
74
+ public BasicErrorController (ErrorAttributes errorAttributes , ErrorProperties errorProperties ,
75
+ WebMvcProperties webMvcProperties ) {
76
+ this (errorAttributes , errorProperties , webMvcProperties , Collections .emptyList ());
69
77
}
70
78
71
79
/**
72
80
* Create a new {@link BasicErrorController} instance.
73
81
* @param errorAttributes the error attributes
74
82
* @param errorProperties configuration properties
83
+ * @param webMvcProperties webMvc properties
75
84
* @param errorViewResolvers error view resolvers
76
85
*/
77
86
public BasicErrorController (ErrorAttributes errorAttributes , ErrorProperties errorProperties ,
78
- List <ErrorViewResolver > errorViewResolvers ) {
87
+ WebMvcProperties webMvcProperties , List <ErrorViewResolver > errorViewResolvers ) {
79
88
super (errorAttributes , errorViewResolvers );
80
89
Assert .notNull (errorProperties , "ErrorProperties must not be null" );
81
90
this .errorProperties = errorProperties ;
91
+ Assert .notNull (webMvcProperties , "WebMvcProperties must not be null" );
92
+ this .webMvcProperties = webMvcProperties ;
82
93
}
83
94
84
95
@ RequestMapping (produces = MediaType .TEXT_HTML_VALUE )
@@ -92,13 +103,23 @@ public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse re
92
103
}
93
104
94
105
@ RequestMapping
95
- public ResponseEntity <Map < String , Object > > error (HttpServletRequest request ) {
106
+ public ResponseEntity <? > error (HttpServletRequest request ) {
96
107
HttpStatus status = getStatus (request );
97
108
if (status == HttpStatus .NO_CONTENT ) {
98
109
return new ResponseEntity <>(status );
99
110
}
100
111
Map <String , Object > body = getErrorAttributes (request , getErrorAttributeOptions (request , MediaType .ALL ));
101
- return new ResponseEntity <>(body , status );
112
+ if (this .webMvcProperties .getProblemdetails ().isEnabled ()) {
113
+ String detail = (body .get ("message" ) != null ) ? body .get ("message" ).toString () : "" ;
114
+ ProblemDetail pd = ProblemDetail .forStatusAndDetail (status , detail );
115
+ if (body .get ("path" ) != null ) {
116
+ pd .setInstance (URI .create (body .get ("path" ).toString ()));
117
+ }
118
+ return ResponseEntity .status (status ).body (pd );
119
+ }
120
+ else {
121
+ return new ResponseEntity <>(body , status );
122
+ }
102
123
}
103
124
104
125
@ ExceptionHandler (HttpMediaTypeNotAcceptableException .class )
0 commit comments