Browse Source

init

master
Lierbao 1 year ago
parent
commit
5f38b02d35
2 changed files with 118 additions and 0 deletions
  1. +62
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/handler/GlobalExceptionHandler.java
  2. +56
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/handler/GlobalResponseHandler.java

+ 62
- 0
pmapi/src/main/java/com/ningdatech/pmapi/common/handler/GlobalExceptionHandler.java View File

@@ -0,0 +1,62 @@
package com.ningdatech.pmapi.common.handler;

import com.ningdatech.basic.enumeration.Status;
import com.ningdatech.basic.model.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
* @description: 统一错误处理
* @author: liuxinxin
* @date: 2023/01/03 11:39
*/
@Slf4j
@ControllerAdvice
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GlobalExceptionHandler {

@ResponseBody
@ExceptionHandler(value = NoHandlerFoundException.class)
public ApiResponse<Void> noHandlerFoundException(NoHandlerFoundException e) {
log.error("【全局异常拦截】NoHandlerFoundException: 请求方法 {}, 请求路径 {}", e.getRequestURL(), e.getHttpMethod());
return ApiResponse.ofStatus(Status.REQUEST_NOT_FOUND);
}

@ResponseBody
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
public ApiResponse<Void> bindException(BindException e) {
String msg = e.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(","));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg, null);
}

@ResponseBody
@ExceptionHandler(value = ConstraintViolationException.class)
public ApiResponse<Void> constraintViolationException(ConstraintViolationException e) {
String msg = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining(","));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg, null);
}

@ResponseBody
@ExceptionHandler(value = Exception.class)
public ApiResponse<Void> handlerException(Exception e) {
log.error("【全局异常拦截】: 异常信息 {}: {} ", e.getClass().getSimpleName(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), e.getMessage(), null);
}

}

+ 56
- 0
pmapi/src/main/java/com/ningdatech/pmapi/common/handler/GlobalResponseHandler.java View File

@@ -0,0 +1,56 @@
package com.ningdatech.pmapi.common.handler;

import cn.hutool.json.JSONUtil;
import com.ningdatech.basic.model.ApiResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
* @Author liuxinxin
* @Date 2021/7/21 11:26
* @Version 1.0
**/
@RestControllerAdvice(basePackages = {})
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> {

private static final String SWAGGER_CLASS_PREFIX = "springfox.documentation";

@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return filter(methodParameter);
}

@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
ApiResponse<Object> apiResponse = ApiResponse.ofSuccess(o);
// 处理字符串时,遇到了类型转换的问题,debug一步一步跟踪,原来是对于字符串的ContentType是“text-plain”,
// ConverterType是StringHttpMessageConverter这个类型转换,
// 由于将结果封装成了自定义的ApiResponse类型,所以有ApiResponse转换成String报错
// 所以需要对String类型的返回值单独进行处理
if (o instanceof String) {
return JSONUtil.toJsonStr(apiResponse);
}
return ApiResponse.ofSuccess(o);
}

private Boolean filter(MethodParameter methodParameter) {
Class<?> declaringClass = methodParameter.getDeclaringClass();
// swagger中的所有返回不进行统一封装
if (declaringClass.getName().contains(SWAGGER_CLASS_PREFIX)) {
return false;
}
if (methodParameter.hasMethodAnnotation(ExceptionHandler.class)) {
return false;
}
// 如果本身就是使用ApiResponse返回,则不需要进行格式化
return !methodParameter.getParameterType().equals(ApiResponse.class);
}
}

Loading…
Cancel
Save