From b0080b6547a03d012dfc252b4d4431988cd81fe9 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 23 Sep 2020 14:43:19 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E7=BB=9F=E4=B8=80=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=97=B6=E6=9C=8D=E5=8A=A1=E8=BF=94=E5=9B=9E=E7=9A=84JSON?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/ErrorCode.java | 6 ++- .../dispose/controller/ErrorController.java | 48 +++++++++++++++++++ .../ControllerNotSupportException.java | 29 +++++++++++ .../exception/GlobalExceptionHandler.java | 4 ++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/dispose/controller/ErrorController.java create mode 100644 src/main/java/com/dispose/exception/ControllerNotSupportException.java diff --git a/src/main/java/com/dispose/common/ErrorCode.java b/src/main/java/com/dispose/common/ErrorCode.java index 4291124b..d5484b64 100644 --- a/src/main/java/com/dispose/common/ErrorCode.java +++ b/src/main/java/com/dispose/common/ErrorCode.java @@ -164,7 +164,10 @@ public enum ErrorCode { * The Err untrusttoken. */ ERR_UNTRUSTTOKEN(38, "未经授权的Token"), - + /** + * Err unknowninterface error code. + */ + ERR_UNKNOWNINTERFACE(39, "未提供该接口"), /** * The Err decrypt base 64. */ @@ -269,6 +272,7 @@ public enum ErrorCode { case ERR_INPUTMISS: return HttpServletResponse.SC_BAD_REQUEST; case ERR_UNSUPPORT: + case ERR_UNKNOWNINTERFACE: return HttpServletResponse.SC_METHOD_NOT_ALLOWED; default: return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; diff --git a/src/main/java/com/dispose/controller/ErrorController.java b/src/main/java/com/dispose/controller/ErrorController.java new file mode 100644 index 00000000..a139093f --- /dev/null +++ b/src/main/java/com/dispose/controller/ErrorController.java @@ -0,0 +1,48 @@ +package com.dispose.controller; + +import com.dispose.common.ErrorCode; +import com.dispose.exception.ControllerNotSupportException; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; + +/** + * The type Error controller. + * + * @author + */ +@Controller +public class ErrorController extends BasicErrorController { + /** + * Instantiates a new Error controller. + * + * @param serverProperties the server properties + */ + public ErrorController(ServerProperties serverProperties) { + super(new DefaultErrorAttributes(), serverProperties.getError()); + } + + /** + * Error response entity. + * + * @param request the request + * @return the response entity + */ + @Override + public ResponseEntity> error(HttpServletRequest request) { + HttpStatus status = getStatus(request); + + if (status.value() == HttpServletResponse.SC_NOT_FOUND) { + throw new ControllerNotSupportException(ErrorCode.ERR_UNKNOWNINTERFACE); + } + + return super.error(request); + } +} diff --git a/src/main/java/com/dispose/exception/ControllerNotSupportException.java b/src/main/java/com/dispose/exception/ControllerNotSupportException.java new file mode 100644 index 00000000..274025b9 --- /dev/null +++ b/src/main/java/com/dispose/exception/ControllerNotSupportException.java @@ -0,0 +1,29 @@ +package com.dispose.exception; + +import com.dispose.common.ErrorCode; +import lombok.Getter; +import lombok.Setter; + +/** + * The type Controller not support exception. + * + * @author + */ +@Getter +@Setter +public class ControllerNotSupportException extends RuntimeException { + /** + * The Err. + */ + private ErrorCode err; + + /** + * Instantiates a new Security protocol exception. + * + * @param err the err + */ + public ControllerNotSupportException(ErrorCode err) { + super(); + this.err = err; + } +} diff --git a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java index 1abd0140..82c70aa3 100644 --- a/src/main/java/com/dispose/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/dispose/exception/GlobalExceptionHandler.java @@ -61,6 +61,10 @@ public class GlobalExceptionHandler { return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(), new String[]{((SecurityProtocolException) ex).getErr().getMsg()}); + } else if (ex instanceof ControllerNotSupportException) { + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, + ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(), + new String[]{((ControllerNotSupportException) ex).getErr().getMsg()}); } else { return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),