COLA + Dubbo 3 + JPA 服务接口,校验异常处理

COLA + Dubbo 3 + JPA 服务接口,校验异常处理

Demo

技术要点

Dubbo protostuff 序列化,JPA 审计/自定义主键/Json字段处理
Service validation 校验, 可乐异常,BCrypt加盐,QueryDSL/blazebit 整合
Hutool BeanUtil 拷贝/DesensitizedUtil 脱敏/RegexPool 正则池/IdUtil 雪花id

领域分层

根据可乐整洁架构分为,app/client/common/domain/infra

代码展示

app

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
   
    private static final String DEFAULT_ERR_CODE = "BIZ_ERROR";

    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response methodNotAllowed() {
   
        return SingleResponse.buildFailure(HttpStatus.METHOD_NOT_ALLOWED.value() + "", HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public Response notFound() {
   
        return SingleResponse.buildFailure(HttpStatus.NOT_FOUND.value() + "", HttpStatus.NOT_FOUND.getReasonPhrase());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Response httpMessageNotReadableException(HttpMessageNotReadableException exception) {
   
        String stackTraceAsString = Throwables.getStackTraceAsString(exception);
        log.error("InvalidFormatException: " + stackTraceAsString);
        return SingleResponse.buildFailure(DEFAULT_ERR_CODE, exception.getMessage());
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Response missingServletRequestParameterException(MissingServletRequestParameterException exception) {
   
        String stackTraceAsString = Throwables.getStackTraceAsString(exception);
        log.error("InvalidFormatException: " + stackTraceAsString);
        return SingleResponse.buildFailure(DEFAULT_ERR_CODE, exception.getMessage());
    }

    @ExceptionHandler(BizException.class)
    public Response bizException(BizException e) {
   
        log.error("业务异常:", e);
        return SingleResponse.buildFailure(e.getErrCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Response exception(Exception e) {
   
        log.error("总异常:", e);
        return SingleResponse.buildFailure(DEFAULT_ERR_CODE, e.getMessage());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Response validationException(ConstraintViolationException e) {
   
        log.warn("Service 校验异常:", e);
        String errMessage = e.getConstraintViolations().stream().findFirst().map(ConstraintViolation::getMessage).get();
        return SingleResponse.buildFailure(DEFAULT_ERR_CODE, errMessage);
    }

    @ExceptionHandler(BindException.class)
    public Response bindException(BindException e) {
   
        log.warn("Controller 校验异常:", e);
        String errMessage = e.getBindingResult().getAllErrors().stream().findFirst().map(DefaultMessageSourceResolvable::getDefaultMessage).get();
        return SingleResponse.buildFailure(DEFAULT_ERR_CODE, errMessage);
    }
}
@Slf4j
@RestController
public class AccountController {
   

    @Setter(onMethod_ = {
   @DubboReference(timeout = 300000, loadbalance = LoadbalanceRules.RANDOM)})
    private AccountService accountService;

    @PostMapping("/addAccount")
    public Response addAccount(@RequestBody AccountAddCmd accountAddCmd) {
   
        Account account = accountService.addAccount(accountAddCmd);
        return SingleResponse.of(account);
    }

    @PostMapping("/removeAccountById")
    public Response removeAccountById(@RequestParam Long id) {
   
        accountService.removeAccountById(id);
        return SingleResponse.buildSuccess();
    }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值