5 min
zh-CN java项目中的一些通用处理
java项目中的一些通用处理
一、全局异常处理#
/**
* 统一异常处理、数据预处理等
*/
@ControllerAdvice
public class ControllerExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);
/**
* 所有异常统一处理,出现这个情况很可能有bug
* @param e
* @return
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
//状态码自定义,出现这个状态码大概率就有未处理异常或者bug
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public CommonResp exceptionHandler(Exception e) {
CommonResp commonResp = new CommonResp();
LOG.error("系统异常:", e);
commonResp.setSuccess(false);
commonResp.setMessage("系统出现异常,请联系管理员");
return commonResp;
}
/**
* 所有自定义异常统一处理
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
@ResponseBody
public CommonResp businessExceptionHandler(BusinessException e) {
CommonResp commonResp = new CommonResp();
LOG.error("业务异常:", e);
commonResp.setSuccess(false);
commonResp.setMessage(e.getMessage());
return commonResp;
}
/**
* 校验异常统一处理
* @param e
* @return
*/
@ExceptionHandler(value = BindException.class)
@ResponseBody
public CommonResp validExceptionHandler(BindException e) {
CommonResp commonResp = new CommonResp();
LOG.warn("参数校验失败:{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
commonResp.setSuccess(false);
commonResp.setMessage(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
return commonResp;
}
}
//自定义异常
public class BusinessException extends RuntimeException {
private BusinessExceptionEnum exceptionEnum;
public BusinessException(BusinessExceptionEnum exceptionEnum) {
super(exceptionEnum.getMessage());
this.exceptionEnum = exceptionEnum;
}
public BusinessExceptionEnum getExceptionEnum() {
return exceptionEnum;
}
public void setExceptionEnum(BusinessExceptionEnum exceptionEnum) {
this.exceptionEnum = exceptionEnum;
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}java二、全局返回值处理#
实现返回值处理接口
public class ApiResponseHandlerMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
private final HandlerMethodReturnValueHandler handler;
private static final HashSet<String> URL_SET = new HashSet<>() {{
add("/alipay/pay");
add("/wechat/pay");
}};
public ApiResponseHandlerMethodReturnValueHandler(HandlerMethodReturnValueHandler handler) {
this.handler = handler;
}
@Override
public boolean supportsReturnType(MethodParameter methodParameter) {
return this.handler.supportsReturnType(methodParameter);
}
@Override
public void handleReturnValue(Object returnValue,
MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer,
NativeWebRequest nativeWebRequest) throws Exception {
String servletPath = nativeWebRequest.getNativeRequest(HttpServletRequest.class).getServletPath();
// 这里根据请求路径做一些处理,比如支付宝、微信支付等接口,对返回值有明确要求的接口路径。保持原返回格式
//todo: 所有的支付接口,都需要
if (URL_SET.contains(servletPath)) {
this.handler.handleReturnValue(returnValue, methodParameter, modelAndViewContainer, nativeWebRequest);
return;
}
// 已经被包装过的CommonResp返回值,直接返回
if (returnValue instanceof CommonResp<?> value) {
this.handler.handleReturnValue(returnValue, methodParameter, modelAndViewContainer, nativeWebRequest);
return;
}
// 其他接口返回值,包装成CommonResp返回值
this.handleReturnValue(CommonResp.success(returnValue), methodParameter, modelAndViewContainer, nativeWebRequest);
}
}java替换原有的返回值处理器
@Configuration
public class WebMvcConfiguration {
@Autowired
public void resetRequestMappingHandlerAdapter(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
var oldReturnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
assert oldReturnValueHandlers != null;
List<HandlerMethodReturnValueHandler> list = new ArrayList<>(oldReturnValueHandlers.size());
for (HandlerMethodReturnValueHandler returnValueHandler : oldReturnValueHandlers) {
// 替换原有的返回值处理器
if (returnValueHandler instanceof RequestResponseBodyMethodProcessor) {
list.add(new ApiResponseHandlerMethodReturnValueHandler(returnValueHandler));
} else {
list.add(returnValueHandler);
}
}
requestMappingHandlerAdapter.setReturnValueHandlers(list);
}
}java3、序列化替换jackson为fastjson2#
-
添加Fastjson2依赖: 首先,在你的
pom.xml文件中添加Fastjson2的依赖。
xml<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson2</artifactId> <version>2.0.2</version> </dependency> <dependency> <artifactId>fastjson2-extension-spring6</artifactId> <groupId>com.alibaba.fastjson2</groupId> <version>2.0.2</version> </dependency> -
配置Fastjson2为默认的序列化工具:
创建一个配置类,用于替换Spring Boot默认的Jackson为Fastjson2。
javaimport com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; @Configuration public class FastJsonConfiguration { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1. 需要定义一个convert转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2. 添加fastJson的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); fastConverter.setFastJsonConfig(fastJsonConfig); // 3. 在convert中添加配置信息; return new HttpMessageConverters(fastConverter); } } -
移除Jackson依赖(可选): 如果你不希望Spring Boot项目中包含Jackson依赖,防止用到了默认的jackson后与预期的序列化不一致,可以在
pom.xml中排除Jackson依赖。
xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency>
通过以上步骤,你就可以将Spring Boot默认的Jackson序列化工具替换为Fastjson2。这样,Spring Boot应用在处理HTTP请求和响应时,将使用Fastjson2进行序列化和反序列化。