validator框架精确返回验证错误信息

import com.cusc.onstar.base.dto.RestResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;

@RestControllerAdvice
public class GlobalExceptionHandler {
private final Logger logger = LoggerFactory.getLogger(getClass());

@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestResult exceptionHandler(Exception e) {
    logger.error("服务异常",e);
    return RestResultGenerator.genErrorResult("服务异常");
}

@ExceptionHandler(value = HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestResult httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
    logger.error("访问参数异常!",e);
    return RestResultGenerator.genErrorResult("访问参数异常");
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestResult illegalParamsExceptionHandler(MethodArgumentNotValidException e) {
    logger.error("---------> invalid request!", e);
    //validator框架精确返回验证错误信息
    List<ObjectError> errors = e.getBindingResult().getAllErrors();
    List<String> defaultMsgs = new ArrayList<>();
    errors.forEach(w-> defaultMsgs.add(w.getDefaultMessage()));
    return RestResultGenerator.genErrorResult(CollectionUtils.isNotEmpty(defaultMsgs) ? StringUtils.join(defaultMsgs.toArray(),",") : "请求参数不合法");
}

}

public class Role {
@NotNull(groups = Second.class,message = “角色对应的id不能为空”)
private Integer id;

@NotEmpty(groups = {First.class,Second.class})
@Pattern(regexp = "^[^\\s]*$",message = "角色名不能为空,且首尾不能包含空格",groups ={First.class,Second.class} )
@Length(max = 5,message = "角色名过长",groups ={First.class,Second.class})
private String roleName;

private String note;

private Integer status;

private Date createdTime;

private Date updatedTime;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getRoleName() {
    return roleName;
}

public void setRoleName(String roleName) {
    this.roleName = roleName;
}

public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

public Date getCreatedTime() {
    return createdTime;
}

public void setCreatedTime(Date createdTime) {
    this.createdTime = createdTime;
}

public Date getUpdatedTime() {
    return updatedTime;
}

public void setUpdatedTime(Date updatedTime) {
    this.updatedTime = updatedTime;
}

}

猜你喜欢

转载自blog.csdn.net/qq_35337467/article/details/80334784