springboot中自定义异常及接口异常处理

SpringBoot默认的错误处理直接回返回对应的错误视图页面。比如404、500错误,但是在实际的项目中是不允许出现500错误的,而且返回错误也是需要给出提示的。

一般情况下,可以通过if 判断条件来过滤,避免异常的发生

       //判断是否重复添加
        if (memberList.contains(String.valueOf(operatorUid))) {
            joinConversationAns.setResultCode(102);
            joinConversationAns.setResultString("You're already in the conversation");
            return joinConversationAns;
        }

但是在一个复杂的接口中,判断的数据非常多,写很多的if else,后期会变得非常不好维护,而且也很不美观。

那么在spingboot中,可以可以做自定义异常处理

首先是定义异常类

public class MsgArgumentException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    protected final String message;

    public MsgArgumentException(String message) {
        this.message = message;
    }

    public MsgArgumentException(String message, Throwable e) {
        super(message, e);
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }


}

然后处理异常,给出返回值和返回码


@ControllerAdvice
public class MsgArgumentExceptionHandler {

    @ExceptionHandler(value = MsgArgumentException.class)
    @ResponseBody
    public ClientSendMsgToServerAns errorHandler(MsgArgumentException e) {
        e.printStackTrace();
        ClientSendMsgToServerAns clientSendMsgToServerAns = new ClientSendMsgToServerAns();
        clientSendMsgToServerAns.setResultCode(101);
        clientSendMsgToServerAns.setResultString("消息参数错误");
        return  clientSendMsgToServerAns;
    }
}

 

然后在业务中,只要有校验不通过的代码,我们可以放心大胆的throw 异常 了,不要怕前端返回500,例如下面针对前端的参数做校验。

 switch (msgType) {
            case MsgTypeCode.TEXT_MESSAGE:
                msgBuild.setTextMsg(msgInfo.getTextMsg());
                return msgBuild.build();

            case MsgTypeCode.IMAGE_MESSAGE:
                ImageMsgInfo imageMsgInfo = msgInfo.getImage_msg();

                if (
                        StringUtils.isEmpty(imageMsgInfo.getUrl())
                                || StringUtils.isEmpty(imageMsgInfo.getName())
                                || imageMsgInfo.getWeight() == 0
                                || imageMsgInfo.getHeight() == 0
                                || StringUtils.isEmpty(imageMsgInfo.getFormat())
                                || imageMsgInfo.getSize() == 0
                ) {
                    throw new MsgArgumentException("参数校验错误");
                }


                AoeImMessage.ImageMsgInfo.Builder imageBuild = AoeImMessage.ImageMsgInfo.newBuilder();
                imageBuild.setUrl(imageMsgInfo.getUrl());
                imageBuild.setName(imageMsgInfo.getName());
                imageBuild.setWeight(imageMsgInfo.getWeight());
                imageBuild.setHeight(imageMsgInfo.getHeight());
                imageBuild.setFormat(imageMsgInfo.getFormat());
                imageBuild.setSize(imageMsgInfo.getSize());
                imageBuild.setExtra(imageMsgInfo.getExtra());
                msgBuild.setImageMsg(imageBuild);

                return msgBuild.build();

            case MsgTypeCode.AUDIO_MESSAGE:
                AudioMsgInfo audioMsgInfo = msgInfo.getAudio_msg();

                if (
                        StringUtils.isEmpty(audioMsgInfo.getUrl())
                                || StringUtils.isEmpty(audioMsgInfo.getName())
                                || StringUtils.isEmpty(audioMsgInfo.getFormat())
                                || audioMsgInfo.getDuration() == 0
                                || audioMsgInfo.getSize() == 0
                ) {
                    throw new MsgArgumentException("参数校验错误");
                }


                AoeImMessage.AudioMsgInfo.Builder audioBuild = AoeImMessage.AudioMsgInfo.newBuilder();
                audioBuild.setUrl(audioMsgInfo.getUrl());
                audioBuild.setName(audioMsgInfo.getName());
                audioBuild.setDuration(audioMsgInfo.getDuration());
                audioBuild.setFormat(audioMsgInfo.getFormat());
                audioBuild.setSize(audioMsgInfo.getSize());
                audioBuild.setExtra(audioMsgInfo.getExtra());
                msgBuild.setAudioMsg(audioBuild);
                return msgBuild.build();

            case MsgTypeCode.VIDEO_MESSAGE:
                VideoMsgInfo videoMsgInfo = msgInfo.getVideo_msg();

                if (
                        StringUtils.isEmpty(videoMsgInfo.getUrl())
                                || StringUtils.isEmpty(videoMsgInfo.getName())
                                || StringUtils.isEmpty(videoMsgInfo.getImageUrl())
                                || StringUtils.isEmpty(videoMsgInfo.getFormat())
                                || videoMsgInfo.getDuration() == 0
                                || videoMsgInfo.getSize() == 0
                                || videoMsgInfo.getWeight() == 0
                                || videoMsgInfo.getHeight() == 0
                ) {
                    throw new MsgArgumentException("参数校验错误");
                }


                AoeImMessage.VideoMsgInfo.Builder videoBuild = AoeImMessage.VideoMsgInfo.newBuilder();
                videoBuild.setUrl(videoMsgInfo.getUrl());
                videoBuild.setName(videoMsgInfo.getName());
                videoBuild.setImageUrl(videoMsgInfo.getImageUrl());
                videoBuild.setDuration(videoMsgInfo.getDuration());
                videoBuild.setFormat(videoMsgInfo.getFormat());
                videoBuild.setSize(videoMsgInfo.getSize());
                videoBuild.setHeight(videoMsgInfo.getHeight());
                videoBuild.setWeight(videoMsgInfo.getWeight());
                videoBuild.setExtra(videoMsgInfo.getExtra());
                msgBuild.setVideoMsg(videoBuild);
                return msgBuild.build();

当前端的参数不通过时,后台会自动处理异常,很省心。

springboot中自定义异常及接口异常处理_第1张图片

你可能感兴趣的:(springboot)