020 Step 12 - Implementing Generic Exception Handling for all Resources

03 - Restful Web Services with Spring Boot - V2 Изменения кода для следующего шага - https://github.com/in28minutes/spring-microservices-v2/blob/main/02.restful-web-services/01-step-by-step-changes/v2.md#step-12 Snippets package com.in28minutes.rest.webservices.restfulwebservices.exception; import java.time.LocalDateTime; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import com.in28minutes.rest.webservices.restfulwebservices.user.UserNotFoundException; @ControllerAdvice public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{ @ExceptionHandler(Exception.class) public final ResponseEntity handleAllExceptions(Exception ex, WebRequest request) throws Exception { ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(UserNotFoundException.class) public final ResponseEntity handleUserNotFoundException(Exception ex, WebRequest request) throws Exception { ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity(errorDetails, HttpStatus.NOT_FOUND); } }

Иконка канала BackDev
58 подписчиков
12+
8 просмотров
2 года назад
12+
8 просмотров
2 года назад

03 - Restful Web Services with Spring Boot - V2 Изменения кода для следующего шага - https://github.com/in28minutes/spring-microservices-v2/blob/main/02.restful-web-services/01-step-by-step-changes/v2.md#step-12 Snippets package com.in28minutes.rest.webservices.restfulwebservices.exception; import java.time.LocalDateTime; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import com.in28minutes.rest.webservices.restfulwebservices.user.UserNotFoundException; @ControllerAdvice public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{ @ExceptionHandler(Exception.class) public final ResponseEntity handleAllExceptions(Exception ex, WebRequest request) throws Exception { ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(UserNotFoundException.class) public final ResponseEntity handleUserNotFoundException(Exception ex, WebRequest request) throws Exception { ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity(errorDetails, HttpStatus.NOT_FOUND); } }

, чтобы оставлять комментарии