1.pom文件引用

Spring Boot 2.3 1 之前,只需要引用spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot 2.3 1 之后,spring-boot-starter-validation 已经不包括在了 spring-boot-starter-web 中,需要我们手动加上。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
如果是普通 Java 程序
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.9.Final</version>
</dependency>

2.全局异常处理类ExceptionHandler

package com.xinghuo.exception;

import java.security.SecureRandom;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.alibaba.fastjson.JSONObject;
import com.xinghuo.utils.Result;

import lombok.extern.slf4j.Slf4j;

/**
 * 异常处理器
 * @author xietao
 * 2022年6月24日 上午10:47:39
 *
 */
@RestControllerAdvice
@Slf4j
public class XHExceptionHandler {

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(XHException.class)
	public Result<?> handleXHException(XHException e) {
		Result<Object> r = new Result<Object>();
		r.setCode(e.getCode());
		r.setMessage(e.getMessage());
		r.setSuccess(false);
		return r;
	}

	@ExceptionHandler(InvalidTokenException.class)
	public Result<?> handleInvalidTokenException(InvalidTokenException e) {
		log.error("token失效:"+e.getToken());
		return Result.noauth(e.getMessage());
	}

	/**
	 * 处理Content-Type异常
	 */
	@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
	public Result<?> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
		return Result.error("Content-Type设置错误", e.getMessage());
	}
	
	/**
	 * 处理Request method异常
	 */
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public Result<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		return Result.error("请求方式错误", e.getMessage());
	}
	
	/**
	 * 处理validate的校验异常
	 */
	@ExceptionHandler(BindException.class)
	public Result<?> handleBindException(BindException e) {
		return wrapErrors(e.getAllErrors());
	}
	
	/**
	 * 处理RequestBody validate的校验异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Result<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
		return wrapErrors(e.getBindingResult().getAllErrors());
	}
	
	/**
	 * 处理参数列表字段validate的校验异常
	 */
	@ExceptionHandler(ConstraintViolationException.class)
	public Result<?> handleConstraintViolationException(ConstraintViolationException e) {
		return wrapErrors(e.getMessage());
	}
	
	@ExceptionHandler(Exception.class)
	public Result<?> handleException(Exception e) {
		log.error(e.getMessage(), e);

		//记录堆栈信息
//		StringBuffer sb = new StringBuffer();
//		sb.append(e.toString() + "\n");
//		StackTraceElement[] stackArray = e.getStackTrace();
//
//		for(int i = 0; i < stackArray.length; ++i) {
//			StackTraceElement element = stackArray[i];
//			sb.append(element.toString() + "\n");
//		}
//		System.out.println("这是异常堆栈信息:"+sb.toString());

		return Result.error("系统出错");
	}
	
	/**
	 * 返回validate异常提示
	 * @param errors
	 * @return
	 */
	private Result<?> wrapErrors(List<ObjectError> errors) {
		if(CollectionUtils.isEmpty(errors)){
			return Result.error("请求参数错误");
		}
		
		//validate的校验错误提示
		SecureRandom random = new SecureRandom();
		JSONObject json = new JSONObject();
		errors.forEach(error->{
			if(error instanceof FieldError) {
				FieldError fieldError = (FieldError)error;
				json.put(fieldError.getField(), error.getDefaultMessage());
			} else {
				json.put(error.getObjectName()+random.nextInt(), error.getDefaultMessage());
			}
		});
		
		return Result.error("请求参数错误", json);
	}
	
	/**
	 * 返回validate异常提示
	 * @param errors
	 * @return
	 */
	private Result<?> wrapErrors(String message) {
		if(StringUtils.isBlank(message)){
			return Result.error("请求参数错误");
		}
		
		//validate的校验错误提示
		if (message.indexOf(".") == -1 || message.indexOf(":") == -1) {
			return Result.error("请求参数错误");
		}
		
		String prefix = message.split("\\.")[0];
		message = message.replace(prefix + ".", "");
		
		JSONObject json = new JSONObject();
		// 判断是否存在多个字段
		if (message.indexOf(",") == -1) {
			String[] msgChildArr = message.split(":");
			json.put(msgChildArr[0].trim(), msgChildArr[1].trim());
		}else{
			String[] msgArr = message.split(",");
			for (String msg : msgArr) {
				String[] msgChildArr = msg.split(":");
				json.put(msgChildArr[0].trim(), msgChildArr[1].trim());
			}
		}
		
		return Result.error("请求参数错误", json);
	}
	
}

3.返回封装类

package com.xinghuo.utils;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 接口返回数据格式
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	@ApiModelProperty(value = "成功标志")
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息")
	private String message = "操作成功!";

	/**
	 * 返回代码
	 */
	@ApiModelProperty(value = "返回代码")
	private Integer code = 0;

	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	private Object result;

	/**
	 * 时间戳
	 */
	@ApiModelProperty(value = "时间戳")
	private long timestamp = System.currentTimeMillis();

	public Result() {

	}

	public Result<T> success(String message) {
		this.message = message;
		this.code = "200";
		this.success = true;
		return this;
	}

	public static Result<Object> ok() {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode("200");
		r.setMessage("成功");
		return r;
	}

	public static Result<Object> ok(String msg) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode("200");
		r.setMessage(msg);
		return r;
	}

	public static Result<Object> okResult(String data) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode("200");
		r.setResult(data);
		return r;
	}

	public static Result<Object> ok(Object data) {
		Result<Object> r = new Result<Object>();

		r.setSuccess(true);
		r.setCode("200");
		r.setResult(data);
		return r;
	}

	public static Result<Object> error(String msg) {
		return error("500", msg);
	}

	public static Result<Object> error(int code, String msg) {
		Result<Object> r = new Result<Object>();
		r.setCode(code);
		r.setMessage(msg);
		r.setSuccess(false);
		return r;
	}

	public static Result<Object> error(String msg, Object result) {
		Result<Object> r = new Result<Object>();
		r.setCode("500");
		r.setMessage(msg);
		r.setSuccess(false);
		r.setResult(result);
		return r;
	}

	/**
	 * 无权限访问返回结果
	 */
	public static Result<Object> noauth(String msg) {
		return error("401", msg);
	}


}

SpringBoot基础框架-Java文档类资源-CSDN下载

注意:如果是spring boot2.0之前,ConstraintViolationException会抛出异常为null,不会展示出错的字段名,不够友好,因为validation版本问题,可以引入高版本

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐