- @RestControllerAdviceむしろ各個別コントローラに定義されているより@ControllerAdvice定義されたクラスを扱う均一な例外を使用して、
- @Restcontrolleradvice:戻り値JSON形式のデータ。
- @ControllerAdvice:バックビュータイプのデータ;
- @ResponseBody:コントローラは、同じ方法を使用して、戻り値はクライアントに転送さJSON方法戻ってきます。
- @ExceptionHandler:関数の例外のタイプを定義するために使用し、最終的にExceptionオブジェクトのURLが要求リターン結果にマッピングされ、注釈のための例外クラス、value属性は、例外の必要性のタイプが傍受指定します。
- アプリケーションを起動した後、ある@ exceptionHandlerの、@ InitBinder、@ ModelAttributeアノテーション付きメソッドは、注釈を@RequestMapping方法に適用されます。
- グローバルキャッチ例外の原則:AOP技術セクションを使用します。
- @RequestBodyでは、JSONが自動的にバインド解決しました。
- AOPの側面を使用してグローバル例外捕捉技術は、ジャーパッケージAOPをインポートする必要があるため
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- パケットのクラスのグローバル例外クラスまたはサブパケットが開始された定義
import com.terse.develop.utils.exception.AdminException;
import com.terse.develop.utils.exception.MsgResultException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.io.IOException;
import java.net.ConnectException;
import java.util.List;
import java.util.Set;
@RestControllerAdvice
@ResponseBody
public class ExceptionHandlerController {
private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlerController1.class);
public static void print(Exception ex) {
logger.error(ex.toString());
ex.printStackTrace();
}
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public Exception HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
print(e);
return new Exception("Http请求方式不支持");
}
@ExceptionHandler(value = {MsgResultException.class, AdminException.class})
public Exception exception(Exception e) {
print(e);
if (e instanceof MsgResultException)
return new MsgResultException(((MsgResultException) e).getMsg());
else
return new AdminException("请重试");
}
@ExceptionHandler(value = HttpMessageNotReadableException.class)
public Exception httpMessageNotReadableException(HttpMessageNotReadableException e) {
print(e);
return new Exception("请检查数据是否正确");
}
@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
public Exception getNumberException(MethodArgumentTypeMismatchException e) {
print(e);
return new Exception("String类型不能转换成数字类型");
}
@ExceptionHandler(value = ConstraintViolationException.class)
public Exception ConstraintViolationExceptionHandler(ConstraintViolationException ex) {
Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
String msg = null;
for (ConstraintViolation<?> cvl : constraintViolations) {
msg = cvl.getMessageTemplate();
}
logger.error(msg);
return new Exception(msg);
}
@ExceptionHandler(value = ServletRequestBindingException.class)
public Exception ServletRequestBindingException(ServletRequestBindingException e) {
print(e);
return new Exception("缺少参数");
}
@ExceptionHandler(value = BindException.class)
public Exception BindExceptionHandler(BindException bindException) {
List<FieldError> fieldErrors = bindException.getFieldErrors();
String msg = null;
for (FieldError fieldError : fieldErrors) {
msg = fieldError.getDefaultMessage();
}
return new Exception(msg);
}
@ExceptionHandler(value = ConnectException.class)
public Exception connectException(ConnectException e) {
print(e);
return new Exception("服务器异常,请联系管理员");
}
@ExceptionHandler(value = IOException.class)
public Exception ioException(IOException e) {
e.printStackTrace();
return new Exception("IO流处理异常,请联系管理员");
}
}