I tried to customize the response at the time of error with Spring Boot, so I will describe it here as a memorandum. In Previous article, I defined an original exception and tried to handle the exception, but this time Spring defines an exception. I would like to describe the handle of.
I was able to catch the original exception defined in the previous article, but when the exception defined by Spring at the beginning of the MethodArgumentNotValidException
exception occurred, the error content was returned with null body part. This time, I will summarize the cause and countermeasure policy.
"A thorough introduction to Spring Java application development with Spring Framework" states as follows. I will quote below.
ResponseEntityExceptionHandler implements @ExceptionHandler method that handles exceptions that occur in Spring MVC framework processing. In other words, you can handle all the exceptions that occur in the framework processing just by creating a class like the one above. If you just create a class that inherits the ResponseEntityExceptionHandler class, the response body will respond with an error in the state of. If you just create a class that inherits ResponseEntityExceptionHandler, the response body will respond empty. Override the handleExceptionInternal method if you want to output error information to the response body.
Now we know the direct cause.
In Previous article, handle handling for original exceptions was implemented, but handle handling is defined for exceptions defined by Spring. I think that the body part was blanked even if an exception occurred because it was not.
(= At the stage of defining the handle class that inherits ResponseEntityExceptionHandler
, it seems that you have to define the handle method not only for your own exception but also when other exceptions occur. I'm not very familiar with this area. Please tell me the details ...)
This time, I checked it in the code base.
If you check the handleMethodArgumentNotValid
method that handles theMethodArgumentNotValidException
of the parent class ResponseEntityExceptionHadler
class, null
is explicitly passed to the body part, and when you debug, this process is passed.
ResponseEntityExceptionHadler.java
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return handleExceptionInternal(ex, null, headers, status, request);
}
For the content and order of the arguments of handleExceptionInternal
, refer to Previous article.
As you can see from the above, null
is explicitly passed to the body part of the method argument, so if you override this method and pass the error content you actually want to display to the body part, it should be displayed. is.
Then, I would like to actually move it.
HelloSpringExceptionController.java
package com.example.demo.springExceptionErrorResponse;
import com.example.demo.errorResponse.ErrorDetail;
import com.example.demo.errorResponse.MyException;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/springexceptionhandle")
public class HelloSpringExceptionController {
@RequestMapping("/test")
public void get(@RequestBody @Validated Person person, Model model) {
}
}
Person.java
package com.example.demo.springExceptionErrorResponse;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class Person {
@NotNull
String firstName;
@NotNull
String lastName;
}
This time I simply passed the string " It's an error. "
`.
Regarding this, please define it as you like by defining a class for the body at the time of error.
SpringExceptionHandler.java
package com.example.demo.springExceptionErrorResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@RestControllerAdvice
public class SpringExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex,
Object object,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
return super.handleExceptionInternal(ex, "It's an error.", headers, status, request);
}
}
Below, try requesting JSON from http: // localhost: 8080 / springexceptionhandle / test
with POST
.
This request should result in an error because Person.java
has a @NotNull
constraint applied to firstName
.
input.json
{
"firstName": null,
"lastName": "name"
}
It's an error.
The above is lonely as a response body, but if you want to display the time when an error occurs, status code, etc., respond like Previous article You should be able to see it by defining a body-specific class and passing an instance of that class as the second argument to the super.handleExceptionInternal
method.
Customize REST API error response with Spring Boot (Part 1)
This time, I referred to the following article when writing the article.
[Thorough introduction to Spring Java application development with Spring Framework](https://www.amazon.co.jp/Spring%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80- Spring-Framework% E3% 81% AB% E3% 82% 88% E3% 82% 8BJava% E3% 82% A2% E3% 83% 97% E3% 83% AA% E3% 82% B1% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E9% 96% 8B% E7% 99% BA-% E6% A0% AA% E5% BC% 8F% E4% BC% 9A % E7% A4% BENTT% E3% 83% 87% E3% 83% BC% E3% 82% BF / dp / 4798142476) Customize the error response of REST API created by Spring Boot Error handling with Spring Boot Handling exceptions with @ RestController of Spring Boot
Recommended Posts