[JAVA] Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3

Overview

--Some constructors and methods of the DefaultErrorAttributes class have been deprecated since Spring Boot 2.3 (not unusable) --Error information can be obtained from DefaultErrorAttributes by using the ErrorAttributeOptions class introduced in Spring Boot 2.3.

Error information that can be obtained with DefaultErrorAttributes

[DefaultErrorAttributes \ (Spring Boot 2 \ .3 \ .0 \ .RELEASE API )](https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/api/org/springframework/boot /web/servlet/error/DefaultErrorAttributes.html) etc., the attributes of the error information that can be acquired are described.

--Timestamp: Time when the error was extracted --status: Status code --error: Reason for error --exception: Root exception class name --message: Exception message --errors: Multiple ObjectErrors (binding-errors) set in BindingResult --trace: Exception stack trace --path: URL path when the exception occurred

Sample code for Spring Boot 2.2

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class ErrorAttributesGetter22 {

  /**
   *Extract error information.
   *
   * @param req request information
   * @return error information
   */
  public static Map<String, Object> getErrorAttributes(HttpServletRequest req) {
    //Get detailed error information with the DefaultErrorAttributes class
    ServletWebRequest swr = new ServletWebRequest(req);
    DefaultErrorAttributes dea = new DefaultErrorAttributes(true);
    return dea.getErrorAttributes(swr, true);
  }
}

Sample code for Spring Boot 2.3

By using the ErrorAttributeOptions class, it is now possible to select whether to acquire some attributes (whether to leave them as error information).

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class ErrorAttributesGetter23 {

  /**
   *Extract error information.
   *
   * @param req request information
   * @return error information
   */
  public static Map<String, Object> getErrorAttributes(HttpServletRequest req) {
    //Get detailed error information with the DefaultErrorAttributes class
    ServletWebRequest swr = new ServletWebRequest(req);
    DefaultErrorAttributes dea = new DefaultErrorAttributes();
    ErrorAttributeOptions eao = ErrorAttributeOptions.of(
      ErrorAttributeOptions.Include.BINDING_ERRORS,
      ErrorAttributeOptions.Include.EXCEPTION,
      ErrorAttributeOptions.Include.MESSAGE,
      ErrorAttributeOptions.Include.STACK_TRACE);
    return dea.getErrorAttributes(swr, eao);
  }
}

Check the source code of DefaultErrorAttributes class of Spring Boot 2.3

Source code of the part deleted from the error information for the item that is not the acquisition target.

spring-boot/DefaultErrorAttributes.java at v2.3.1.RELEASE · spring-projects/spring-boot · GitHub

@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
	Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
	if (this.includeException != null) {
		options = options.including(Include.EXCEPTION);
	}
	if (!options.isIncluded(Include.EXCEPTION)) {
		errorAttributes.remove("exception");
	}
	if (!options.isIncluded(Include.STACK_TRACE)) {
		errorAttributes.remove("trace");
	}
	if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {
		errorAttributes.put("message", "");
	}
	if (!options.isIncluded(Include.BINDING_ERRORS)) {
		errorAttributes.remove("errors");
	}
	return errorAttributes;
}

Reference material

Recommended Posts

Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
[Java] Get data from DB using singleton service in Spring (Boot)
Uploading and downloading files using Ajax in Spring Boot (without JQuery)
Unknown error in line 1 of pom.xml when using Spring Boot in Eclipse
Spring Boot Whitelabel Error Page and JSON Response
Output request and response log in Spring Boot
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Get cookies in Spring
Create a portfolio app using Java and Spring Boot
Testing JPA entities and repositories using Spring Boot @DataJpaTest
[Spring Boot] Get user information with Rest API (beginner)
Test field-injected class in Spring boot test without using Spring container
Get location information in Rails and sort in ascending order
CSRF countermeasure policy and implementation example in REST application using "Spring Boot" + "EXT JS"
Set context-param in Spring Boot
Spring validation and error code
Get EXIF information in Java
Get started with Spring boot
Spring Boot 2 multi-project in Gradle
Get information in an instance and calculate numbers Standard version
How to call and use API in Java (Spring Boot)
Error in Spring database connection
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Try using Spring Boot Security
I tried to get started with Swagger using Spring Boot
8 things to insert into DB using Spring Boot and JPA
How to control transactions in Spring Boot without using @Transactional
Easily develop web applications with STS and Spring Boot. In 10 minutes.
[Java] Get date information 10 days later using milliseconds in the Date class
Get a proxy instance of the component itself in Spring Boot
Fitted in Spring Boot using a bean definition file named application.xml
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Image Spring Boot app using jib-maven-plugin and start it with Docker
Spring Boot Hello World in Eclipse
Spring Boot + PostgreSQL error resolution method
Spring Boot Tutorial Using Spring Security Authentication
[Ethereum] Get block information using web3j
Write test code in Spring Boot
Get validation results with Spring Boot
Store session information in database in Spring Session
Implement REST API in Spring Boot
Spring profile function, and Spring Boot application.properties
What is @Autowired in Spring boot?
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot
Create a Spring Boot project in intellij and exit immediately after launching
Spring Boot + MyBatis I get this error if I don't set the database
I got stuck using snake case for variable name in Spring Boot
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
Using Hystrix and Sentinel in code example
Launch (old) Spring Boot project in IntelliJ
Build Spring Boot + Docker image in Gradle
Static file access priority in Spring boot
Exists using Specification in Spring Data JPA
Local file download memorandum in Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
Error in implementation when implementing Spring validation
[Practice! ] Display Hello World in Spring Boot