Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
cca153e9
Commit
cca153e9
authored
Oct 14, 2015
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document how to return custom JSON on errors
Closes gh-3999
parent
26633533
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
0 deletions
+31
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+31
-0
No files found.
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
cca153e9
...
@@ -1440,6 +1440,37 @@ is to handle `text/html` specifically and provide a fallback for everything else
...
@@ -1440,6 +1440,37 @@ is to handle `text/html` specifically and provide a fallback for everything else
just extend `BasicErrorController` and add a public method with a `@RequestMapping` that
just extend `BasicErrorController` and add a public method with a `@RequestMapping` that
has a `produces` attribute, and create a bean of your new type.
has a `produces` attribute, and create a bean of your new type.
You can also define a `@ControllerAdvice` to customize the JSON document to return for a
particular controller and/or exception type.
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@ControllerAdvice(basePackageClasses = FooController.class)
public class FooControllerAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(YourException.class)
@ResponseBody
ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
HttpStatus status = getStatus(request);
return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
}
----
In the example above, if `YourException` is thrown by a controller defined in the same package
as `FooController`, a json representation of the `CustomerErrorType` POJO will be used instead
of the `ErrorAttributes` representation.
If you want more specific error pages for some conditions, the embedded servlet containers
If you want more specific error pages for some conditions, the embedded servlet containers
support a uniform Java DSL for customizing the error handling. Assuming that you have a
support a uniform Java DSL for customizing the error handling. Assuming that you have a
mapping for `/400`:
mapping for `/400`:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment