如何处理全局自定义异常处理程序。不使用try catch块

时间:2019-07-26 07:54:46

标签: java mongodb spring-boot exception

我正在使用Spring Boot rest API。我正在尝试处理全局自定义异常。而不使用try-catch块。

我正在使用邮递员和内容媒体类型作为multipart / form-data。
但是我没有收到自定义异常类的响应。
这是我的自定义例外类。
但应在邮递员状态,statusCode和错误消息

中给出响应
@SuppressWarnings({ "unchecked" })
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

        @ExceptionHandler(Exception.class)
        public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
            List<String> details = new ArrayList<>();
            details.add(ex.getLocalizedMessage());
            ErrorResponse error = new ErrorResponse("Server Error", details);
            return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
        }

        @ExceptionHandler(DocumentNotFoundException.class)
        public final ResponseEntity<Object> handleUserNotFoundException(DocumentNotFoundException ex, WebRequest request) {
            List<String> details = new ArrayList<>();
            details.add(ex.getLocalizedMessage());
            ErrorResponse error = new ErrorResponse("document Not Found", details);
            return new ResponseEntity(error, HttpStatus.NOT_FOUND);
        }

        @Override
        protected ResponseEntity<Object> 
    handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                HttpHeaders headers, HttpStatus status, WebRequest request) {
            List<String> details = new ArrayList<>();
            for (ObjectError error : ex.getBindingResult().getAllErrors()) {
                details.add(error.getDefaultMessage());
            }
            ErrorResponse error = new ErrorResponse("Validation Failed", details);
            return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
        }
}
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "error")
public class ErrorResponse {
        public ErrorResponse(String message, List<String> details) {
            super();
            this.message = message;
            this.details = details;
        }

        // General error message about nature of error
        private String message;

        // Specific errors in API request processing
        private List<String> details;

        // Getter and setters

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public List<String> getDetails() {
            return details;
        }

        public void setDetails(List<String> details) {
            this.details = details;
        }

}
@GetMapping("/documenttype/{groupId}")
public RestResponse getDocumentTypes(@PathVariable String groupId) {
        List<DocumentType> document = dataserviceImpl.getDocumentList(groupId);
            RestResponse res1 = new RestResponse();
            ResponseEntity<Object> res = ResponseEntity.ok().body(document);
            res1.setMessage("data found");
            res1.setResponse(res);
            return res1;

}

0 个答案:

没有答案