REM:
1. 统一访问不存在的接口时服务返回的JSON内容
This commit is contained in:
HuangXin 2020-09-23 14:43:19 +08:00
parent 7754b775cd
commit b0080b6547
4 changed files with 86 additions and 1 deletions

View File

@ -164,7 +164,10 @@ public enum ErrorCode {
* The Err untrusttoken. * The Err untrusttoken.
*/ */
ERR_UNTRUSTTOKEN(38, "未经授权的Token"), ERR_UNTRUSTTOKEN(38, "未经授权的Token"),
/**
* Err unknowninterface error code.
*/
ERR_UNKNOWNINTERFACE(39, "未提供该接口"),
/** /**
* The Err decrypt base 64. * The Err decrypt base 64.
*/ */
@ -269,6 +272,7 @@ public enum ErrorCode {
case ERR_INPUTMISS: case ERR_INPUTMISS:
return HttpServletResponse.SC_BAD_REQUEST; return HttpServletResponse.SC_BAD_REQUEST;
case ERR_UNSUPPORT: case ERR_UNSUPPORT:
case ERR_UNKNOWNINTERFACE:
return HttpServletResponse.SC_METHOD_NOT_ALLOWED; return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
default: default:
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@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<Map<String, Object>> error(HttpServletRequest request) {
HttpStatus status = getStatus(request);
if (status.value() == HttpServletResponse.SC_NOT_FOUND) {
throw new ControllerNotSupportException(ErrorCode.ERR_UNKNOWNINTERFACE);
}
return super.error(request);
}
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@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;
}
}

View File

@ -61,6 +61,10 @@ public class GlobalExceptionHandler {
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION,
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(), ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
new String[]{((SecurityProtocolException) ex).getErr().getMsg()}); 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 { } else {
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION, return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION,
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(), ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),