改进异常处理
This commit is contained in:
+17
-4
@@ -8,22 +8,35 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
import org.springside.examples.bootapi.service.exception.ErrorCode;
|
||||
import org.springside.examples.bootapi.service.exception.ServiceException;
|
||||
import org.springside.modules.web.MediaTypes;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ServiceExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
@ControllerAdvice(annotations = { RestController.class })
|
||||
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = { ServiceException.class })
|
||||
public final ResponseEntity<?> handleException(ServiceException ex, HttpServletRequest request) {
|
||||
public final ResponseEntity<ErrorResult> handleServiceException(ServiceException ex, HttpServletRequest request) {
|
||||
// 注入servletRequest,用于出错时打印请求来源地址
|
||||
logger.error(ex.getMessage() + ", request from " + request.getRemoteAddr(), ex);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.parseMediaType(MediaTypes.JSON_UTF_8));
|
||||
ErrorResult result = new ErrorResult(ex.code.code, ex.getMessage());
|
||||
return new ResponseEntity<Object>(result, headers, HttpStatus.valueOf(ex.code.httpStatus));
|
||||
return new ResponseEntity<ErrorResult>(result, headers, HttpStatus.valueOf(ex.code.httpStatus));
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = { Exception.class })
|
||||
public final ResponseEntity<ErrorResult> handleGeneralException(Exception ex, HttpServletRequest request) {
|
||||
// 注入servletRequest,用于出错时打印请求来源地址
|
||||
logger.error(ex.getMessage() + ", request from " + request.getRemoteAddr(), ex);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.parseMediaType(MediaTypes.JSON_UTF_8));
|
||||
ErrorResult result = new ErrorResult(ErrorCode.INTERNAL_SERVER_ERROR.code, "Internal Server Error");
|
||||
return new ResponseEntity<ErrorResult>(result, headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
public static class ErrorResult {
|
||||
+1
-1
@@ -2,7 +2,7 @@ package org.springside.examples.bootapi.service.exception;
|
||||
|
||||
public enum ErrorCode {
|
||||
|
||||
BAD_REQUEST(1000, 400), UNAUTHORIZED(1001, 401), FORBIDDEN(1002, 403),
|
||||
BAD_REQUEST(400, 400), UNAUTHORIZED(401, 401), FORBIDDEN(403, 403), INTERNAL_SERVER_ERROR(500, 500),
|
||||
|
||||
BOOK_STATUS_WRONG(1100, 400), BOOK_OWNERSHIP_WRONG(1101, 403), NO_TOKEN(1102, 401);
|
||||
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springside.examples.bootapi.BootApiApplication;
|
||||
import org.springside.examples.bootapi.api.support.ServiceExceptionHandler.ErrorResult;
|
||||
import org.springside.examples.bootapi.api.support.CustomExceptionHandler.ErrorResult;
|
||||
import org.springside.examples.bootapi.domain.Book;
|
||||
import org.springside.examples.bootapi.dto.BookDto;
|
||||
import org.springside.examples.bootapi.repository.BookDao;
|
||||
|
||||
Reference in New Issue
Block a user