修复oauth-starter问题

This commit is contained in:
zhuyijun 2022-09-20 23:34:40 +08:00
parent f4e1c7658c
commit d675d07254
3 changed files with 27 additions and 27 deletions

View File

@ -1,5 +1,9 @@
package cn.zyjblogs.starter.common.entity.response;
/**
* @author zhuyijun
*/
public enum HttpCode {
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
@ -128,6 +132,7 @@ public enum HttpCode {
return this.is4xxClientError() || this.is5xxServerError();
}
@Override
public String toString() {
return this.value + " " + this.name();
}

View File

@ -5,6 +5,9 @@
package cn.zyjblogs.starter.common.entity.response;
/**
* @author zhuyijun
*/
public class ResponseResult {
private static final int ZERO_DATA_TOTAL = 0;
private static final int ONE_DATA_TOTAL = 1;
@ -23,22 +26,20 @@ public class ResponseResult {
public static <T> ResponseObject<T> success(long total, T data) {
return ResponseObject.<T>builder().code(HttpCode.OK.value()).msg(HttpCode.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build();
}
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorMsg) {
return error(responseCode, responseCode.getReasonPhrase(), errorMsg, 0L, null);
}
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorCode, String errorMsg) {
return error(responseCode, errorCode, errorMsg, 0L, null);
return error(responseCode, errorMsg, 0L, null);
}
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorCode, String errorMsg, T data) {
return error(responseCode, errorCode, errorMsg, 1L, data);
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorMsg, T data) {
return error(responseCode, errorMsg, 1L, data);
}
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorCode, String errorMsg, long total, T data) {
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorMsg, long total, T data) {
if (responseCode.value() == HttpCode.OK.value()) {
throw new IllegalArgumentException("ResponseResult.error方法中的responseCode参数不能是ResponseCode.REQUEST_SUCCESS");
} else {
return ResponseObject.<T>builder().code(responseCode.value()).errorCode(errorCode).msg(errorMsg == null ? responseCode.getReasonPhrase() : errorMsg).count(total).data(data).timestamp(System.currentTimeMillis()).build();
return ResponseObject.<T>builder().code(responseCode.value()).msg(errorMsg == null ? responseCode.getReasonPhrase() : errorMsg).count(total).data(data).timestamp(System.currentTimeMillis()).build();
}
}
}

View File

@ -8,6 +8,7 @@ package cn.zyjblogs.starter.web.hander;
import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.common.exception.AbstractBusinessException;
import cn.zyjblogs.starter.common.exception.AbstractFrameworkException;
import java.util.List;
import java.util.Set;
@ -66,7 +67,6 @@ public class GlobalExceptionHandler {
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
public ResponseObject<String> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) {
String parameterName = exception.getParameter().getParameterName();
String i18nCode = "web_starter_property_format_error";
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
}
@ -80,7 +80,7 @@ public class GlobalExceptionHandler {
i18nCode = "web_starter_bad_request";
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
} else {
i18nCode = ((ConstraintViolation)constraintViolations.stream().findFirst().get()).getMessage();
i18nCode = constraintViolations.stream().findFirst().get().getMessage();
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
}
}
@ -94,10 +94,13 @@ public class GlobalExceptionHandler {
@ExceptionHandler({AbstractFrameworkException.class})
public ResponseObject<?> handleFrameworkException(AbstractFrameworkException exception) {
log.error("系统异常:", exception);
String i18nCode = "web_starter_internal_server_error";
return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, i18nCode, i18nCode);
return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, exception.getMessage());
}
@ExceptionHandler({AbstractBusinessException.class})
public ResponseObject<?> handleBusinessException(AbstractBusinessException exception) {
log.error("系统异常:", exception);
return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, exception.getMessage());
}
@ExceptionHandler({BindException.class})
public ResponseObject<?> handleBindException(BindException exception) {
BindingResult bindingResult = exception.getBindingResult();
@ -107,26 +110,17 @@ public class GlobalExceptionHandler {
@ExceptionHandler({HttpMessageNotReadableException.class})
public ResponseObject<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) {
log.error("参数格式不正确", exception);
String i18nCode = "web_starter_param_format_error";
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
return ResponseResult.error(HttpCode.BAD_REQUEST, exception.getMessage());
}
private ResponseObject<?> getByBindingResult(BindingResult bindingResult) {
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
if (fieldErrors.isEmpty()) {
log.error("捕获到了参数校验错误但是没有找到错误提示的i18nCode,{}", bindingResult);
String i18nCode = "web_starter_parameter_error";
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
log.error("捕获到了参数校验错误,{}", bindingResult);
return ResponseResult.error(HttpCode.BAD_REQUEST, bindingResult.toString());
} else {
FieldError fieldError = (FieldError)fieldErrors.get(0);
String i18nCode;
if ("typeMismatch".equals(fieldError.getCode())) {
i18nCode = "web_starter_property_format_error";
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
} else {
i18nCode = fieldError.getDefaultMessage();
return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode);
}
FieldError fieldError = fieldErrors.get(0);
return ResponseResult.error(HttpCode.BAD_REQUEST, fieldError.getDefaultMessage());
}
}
}