Skip to content

Commit

Permalink
Merge branch 'feature/1.0.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu committed Aug 21, 2024
2 parents 2895f33 + 2505fc8 commit efcb0f9
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author fuhouyu
* @since 2024/8/13 17:39
*/
public interface ResponseCodeStatus {
public interface ResponseCode {

/**
* 获取响应码
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author fuhouyu
* @since 2024/8/13 17:40
*/
public enum ResponseCodeStatusEnum implements ResponseCodeStatus {
public enum ResponseCodeEnum implements ResponseCode {

// 200

Expand Down Expand Up @@ -54,6 +54,11 @@ public enum ResponseCodeStatusEnum implements ResponseCodeStatus {
*/
METHOD_NOT_ALLOWED(405, "不支持的方法"),

/**
* 不支持的媒体类型
*/
NOT_SUPPORT_MEDIA_TYPE(415, "不支持的媒体类型"),

/**
* 服务内部错误
*/
Expand All @@ -66,7 +71,7 @@ public enum ResponseCodeStatusEnum implements ResponseCodeStatus {
private final String message;


ResponseCodeStatusEnum(int code, String message) {
ResponseCodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private ResponseHelper() {

public static <T> RestResult<T> success(T data) {
return RestResult.<T>builder()
.withResponseCodeStatus(ResponseCodeStatusEnum.SUCCESS)
.withResponseCodeStatus(ResponseCodeEnum.SUCCESS)
.withData(data).build();
}

Expand All @@ -47,11 +47,11 @@ public static <T> RestResult<T> failed(int code, String message) {
.build();
}

public static <T> RestResult<T> failed(ResponseCodeStatus responseCodeStatus) {
return failed(responseCodeStatus.getCode(), responseCodeStatus.getMessage());
public static <T> RestResult<T> failed(ResponseCode responseCode) {
return failed(responseCode.getCode(), responseCode.getMessage());
}

public static <T> RestResult<T> failed(ResponseCodeStatus responseCodeStatus, String message) {
return failed(responseCodeStatus.getCode(), message);
public static <T> RestResult<T> failed(ResponseCode responseCode, String message) {
return failed(responseCode.getCode(), message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public static final class BaseResponseBuilder<T> {
private BaseResponseBuilder() {
}

public BaseResponseBuilder<T> withResponseCodeStatus(ResponseCodeStatus responseCodeStatus) {
this.code = responseCodeStatus.getCode();
this.message = responseCodeStatus.getMessage();
public BaseResponseBuilder<T> withResponseCodeStatus(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.message = responseCode.getMessage();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.fuhouyu.framework.security.core.filter;

import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum;
import com.fuhouyu.framework.model.response.ResponseCodeEnum;
import com.fuhouyu.framework.model.response.ResponseHelper;
import com.fuhouyu.framework.model.response.RestResult;
import com.fuhouyu.framework.utils.JacksonUtil;
Expand All @@ -43,7 +43,7 @@ public class ApplicationBasicErrorFilter extends BasicAuthenticationFilter {
*/
public ApplicationBasicErrorFilter(AuthenticationManager authenticationManager) {
super(authenticationManager, (request, response, authException) -> {
RestResult<String> restResult = ResponseHelper.failed(ResponseCodeStatusEnum.NOT_AUTH, authException.getMessage());
RestResult<String> restResult = ResponseHelper.failed(ResponseCodeEnum.NOT_AUTH, authException.getMessage());
String body = JacksonUtil.writeValueAsString(restResult);
try (ServletOutputStream outputStream = response.getOutputStream()) {
outputStream.write(body.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.fuhouyu.framework.web.aspectj;

import com.fuhouyu.framework.cache.service.CacheService;
import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum;
import com.fuhouyu.framework.model.response.ResponseCodeEnum;
import com.fuhouyu.framework.web.annotaions.NoRepeatSubmit;
import com.fuhouyu.framework.web.constants.FormTokenConstant;
import com.fuhouyu.framework.web.exception.WebServiceException;
Expand Down Expand Up @@ -70,14 +70,14 @@ public Object doAround(ProceedingJoinPoint joinPoint, NoRepeatSubmit noRepeatSub
String formTokenHeader = request.getHeader(noRepeatSubmit.headerToken());
if (!StringUtils.hasText(formTokenHeader)) {
throw new WebServiceException(
ResponseCodeStatusEnum.INVALID_PARAM,
ResponseCodeEnum.INVALID_PARAM,
noRepeatSubmit.message());
}
Object formToken = cacheService.get(FormTokenConstant.TOKEN_PREFIX
+ formTokenHeader);
if (Objects.isNull(formToken)) {
throw new WebServiceException(
ResponseCodeStatusEnum.INVALID_PARAM,
ResponseCodeEnum.INVALID_PARAM,
noRepeatSubmit.message());
}
cacheService.delete(FormTokenConstant.TOKEN_PREFIX + formTokenHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.fuhouyu.framework.web.exception;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum;
import com.fuhouyu.framework.model.response.ResponseCodeEnum;
import com.fuhouyu.framework.model.response.ResponseHelper;
import com.fuhouyu.framework.model.response.RestResult;
import com.fuhouyu.framework.utils.JacksonUtil;
Expand All @@ -26,11 +26,15 @@
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.server.ResponseStatusException;
Expand Down Expand Up @@ -65,7 +69,7 @@ public class WebExceptionHandler {
@ExceptionHandler(Exception.class)
public RestResult<Void> exceptionHandle(ServletWebRequest request, Exception e) {
this.printExceptionLog(e, Exception.class.getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.SERVER_ERROR);
return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR);
}

/**
Expand All @@ -78,7 +82,7 @@ public RestResult<Void> exceptionHandle(ServletWebRequest request, Exception e)
@ExceptionHandler(NoResourceFoundException.class)
public RestResult<Void> noResourceFoundException(ServletWebRequest request, NoResourceFoundException e) {
this.printExceptionLog(e, Exception.class.getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.NOT_FOUND,
return ResponseHelper.failed(ResponseCodeEnum.NOT_FOUND,
String.format("当前访问地址:%s 不存在", e.getMessage().replace("No static resource ", "").trim()));
}

Expand All @@ -92,7 +96,7 @@ public RestResult<Void> noResourceFoundException(ServletWebRequest request, NoRe
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public RestResult<Void> methodNotSupportException(ServletWebRequest request, HttpRequestMethodNotSupportedException e) {
this.printExceptionLog(e, e.getClass().getName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.METHOD_NOT_ALLOWED);
return ResponseHelper.failed(ResponseCodeEnum.METHOD_NOT_ALLOWED);
}

/**
Expand All @@ -105,20 +109,7 @@ public RestResult<Void> methodNotSupportException(ServletWebRequest request, Htt
@ExceptionHandler(ResponseStatusException.class)
public RestResult<Void> notFoundException(ServletWebRequest request, ResponseStatusException e) {
this.printExceptionLog(e, e.getClass().getName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.SERVER_ERROR, e.getMessage());
}

/**
* 请求参数异常
*
* @param request 请求
* @param e 异常
* @return 包装后的异常信息
*/
@ExceptionHandler(MissingRequestValueException.class)
public RestResult<Void> parameterException(ServletWebRequest request, MissingRequestValueException e) {
this.printExceptionLog(e, e.getClass().getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, e.getMessage());
return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR, e.getMessage());
}

/**
Expand Down Expand Up @@ -146,24 +137,58 @@ public RestResult<Void> methodArgumentNotValidExceptionHandler(ServletWebRequest
errorMessageMap.put(fieldError.getField(), fieldError.getDefaultMessage());
}
this.printExceptionLog(e, e.getClass().getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, JacksonUtil.writeValueAsString(errorMessageMap));
return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, JacksonUtil.writeValueAsString(errorMessageMap));
}


/**
* 请求入参转换异常
* 参数异常都会在这里进行统一抛出。
*
* @param request 请求
* @param e 请求入参转换异常
* @return 包装后的异常信息
* @param e http缺少参数异常
* @return 包装后的响应
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public RestResult<Void> readableException(ServletWebRequest request, HttpMessageNotReadableException e) {
@ExceptionHandler(value = {
MissingServletRequestParameterException.class,
IllegalArgumentException.class,
HttpMessageNotReadableException.class,
MissingRequestValueException.class,
NumberFormatException.class,
HttpMessageConversionException.class
})
public RestResult<String> handleHttpMediaTypeException(ServletWebRequest request,
Exception e) {
this.printExceptionLog(e, e.getClass().getSimpleName(), request);
// 直接返回参数异常
return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, e.getMessage());
return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, e.getMessage());
}

/**
* 任何数据库的异常都会在这里进行抛出
*
* @param e 异常信息
* @param request web请求
* @return 包装后的响应
*/
@ExceptionHandler(value = DataAccessException.class)
public RestResult<String> handleDataAccessException(ServletWebRequest request, DataAccessException e) {
this.printExceptionLog(e, e.getClass().getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR, e.getMessage());
}

/**
* 媒体类型错误
*
* @param request 请求
* @param e 媒体请求异常
* @return 包装后的响应
*/
@ExceptionHandler(HttpMediaTypeException.class)
public RestResult<String> handleHttpMediaTypeException(ServletWebRequest request, HttpMediaTypeException e) {
this.printExceptionLog(e, e.getClass().getSimpleName(), request);
return ResponseHelper.failed(ResponseCodeEnum.NOT_SUPPORT_MEDIA_TYPE, e.getMessage());
}


/**
* 入参的body体中的参数异常拦截器 如 @RequestBody @Size(min = 1, message= "xxx") List<Integer> list
*
Expand All @@ -181,7 +206,7 @@ public RestResult<Void> constraintViolationException(ServletWebRequest request,
sb.append(message).append("\n");
}
// 直接返回参数异常
return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, sb.toString());
return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, sb.toString());
}

/**
Expand All @@ -197,7 +222,7 @@ public void printExceptionLog(Exception e, String exceptionName, ServletWebReque
String requestUrl = request.getRequest().getRequestURI();
LoggerUtil.error(LOGGER,
"""
异常: {}
异常名称: {}
请求方式: {}
请求路径: {}
请求头参数: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.fuhouyu.framework.web.exception;

import com.fuhouyu.framework.model.response.ResponseCodeStatus;
import com.fuhouyu.framework.model.response.ResponseCode;

/**
* <p>
Expand All @@ -32,15 +32,15 @@ public class WebServiceException extends RuntimeException {

private final String message;

private final ResponseCodeStatus responseStatus;
private final ResponseCode responseStatus;

public WebServiceException(ResponseCodeStatus responseStatus) {
public WebServiceException(ResponseCode responseStatus) {
this.responseStatus = responseStatus;
this.status = responseStatus.getCode();
this.message = responseStatus.getMessage();
}

public WebServiceException(ResponseCodeStatus responseStatus, String errorMessage) {
public WebServiceException(ResponseCode responseStatus, String errorMessage) {
this.status = responseStatus.getCode();
this.message = errorMessage;
this.responseStatus = responseStatus;
Expand All @@ -55,7 +55,7 @@ public String getMessage() {
return message;
}

public ResponseCodeStatus getResponseStatus() {
public ResponseCode getResponseStatus() {
return responseStatus;
}
}

0 comments on commit efcb0f9

Please sign in to comment.