异常处理(springboot集成最新)

学成异常处理

1、定义枚举类

  • 定义通用返回消息
  • 每个错误类型都有一个私有的构造函数,用于初始化对应的错误消息。通过公共的 getErrMessage() 方法,可以获取到每个错误类型对应的错误消息。
  • 通过 CommonError.PARAMS_ERROR.getErrMessage(); 获取
public enum CommonError {  
  
    UNKOWN_ERROR("执行过程异常,请重试。"),  
    PARAMS_ERROR("非法参数"),  
    OBJECT_NULL("对象为空"),  
    QUERY_NULL("查询结果为空"),  
    REQUEST_NULL("请求参数为空");  
  
    // 获取枚举类  
    private String errMessage;  
  
    public String getErrMessage() {  
        return errMessage;  
    }  
  
    // 构造方法  
    private CommonError(String errMessage) {  
        this.errMessage = errMessage;  
    }  
}

2、自定义异常

  • 1、定义自定义异常类 PrivateException,继承自 RuntimeException。
  • 2、getErrMessage() 方法:这个方法用于获取 errMessage 属性的值,供外部访问该异常对象的异常消息。
  • 3、无参/有参 构造 调用父类的带有异常消息参数的构造函数,并设置 errMessage 字段。

cast(CommonError commonError) 根据枚举类,抛出异常

CommonError error = CommonError.PARAMS_ERROR;
PrivateException.cast(error);

cast(String errMessage) 根据传入的字符串,抛出异常

PrivateException.cast("Custom error message");
public class PrivateException extends RuntimeException {  
  
    private String errMessage;  
  
    public String getErrMessage() {  
        return errMessage;  
    }  
  
    public PrivateException() {  
        super();  
    }  
  
    public PrivateException(String errMessage) {  
        super(errMessage);  
        this.errMessage = errMessage;  
    }  
  
    public static void cast(CommonError commonError){  
        throw new PrivateException(commonError.getErrMessage());  
    }  
    public static void cast(String errMessage){  
        throw new PrivateException(errMessage);  
    }  
  
}

3、定义错误响应参数

  • 这个文件定义了一个简单的错误响应参数包装类 RestErrorResponse,用于包装错误消息。它包含了一个存储错误消息的字段,并提供了相应的 getter 和 setter 方法。
  • 200 OKHttpStatus.OK
  • 201 CreatedHttpStatus.CREATED
  • 204 No ContentHttpStatus.NO_CONTENT
  • 400 Bad RequestHttpStatus.BAD_REQUEST
  • 401 UnauthorizedHttpStatus.UNAUTHORIZED
  • 403 ForbiddenHttpStatus.FORBIDDEN
  • 404 Not FoundHttpStatus.NOT_FOUND
  • 500 Internal Server ErrorHttpStatus.INTERNAL_SERVER_ERROR

表示 RESTful API 返回的错误响应信息:

return new ResponseEntity<>("响应状态内容", HttpStatus.UNAUTHORIZED);
public class RestErrorResponse implements Serializable {  
  
    private String errMessage;  
  
    public RestErrorResponse(String errMessage){  
        this.errMessage= errMessage;  
    }  
  
    public String getErrMessage() {  
        return errMessage;  
    }  
  
    public void setErrMessage(String errMessage) {  
        this.errMessage = errMessage;  
    }  
}

4、全局异常处理

  • @ControllerAdvice:这是 Spring Framework 提供的注解,用于声明该类为全局异常处理器。全局异常处理器可以捕获应用程序中抛出的异常,并进行统一的处理。
  • customException 方法:处理自定义的 PrivateException 异常的方法。在方法内部,首先记录异常信息到日志中,然后返回一个自定义的错误响应 RestErrorResponse 对象,其中包含了异常的错误消息。
  • exception 方法:这是用来处理其他类型的异常的方法,例如通用的 Exception。在方法内部,同样记录异常信息到日志中,然后返回一个通用的错误响应 RestErrorResponse 对象,其中包含了一个预定义的错误消息。
@Slf4j  
@ControllerAdvice  
public class GlobalExceptionHandler {  
  
    /**  
     * 处理 XueChengPlusException 异常    
*/  
    @ResponseBody  
    @ExceptionHandler(PrivateException.class)  
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)  
    public RestErrorResponse customException(PrivateException e) {  
        log.error("【系统异常】{}",e.getErrMessage(),e);  
        return new RestErrorResponse(e.getErrMessage());  
    }  
  
    /**  
     * 处理其他异常    
*/  
    @ResponseBody  
    @ExceptionHandler(Exception.class)  
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)  
    public RestErrorResponse exception(Exception e) {  
        log.error("【系统异常】{}",e.getMessage(),e); // 记录异常信息到日志中    
return new RestErrorResponse(CommonError.UNKOWN_ERROR.getErrMessage());  
    }  
    /**  
     * 处理检验异常(JSR-303)  
     * methodArgumentNotValidException     */  
    @ResponseBody // 返回值作为响应体返回    
@ExceptionHandler(MethodArgumentNotValidException.class) // 标记该方法处理 Exception 异常    
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 返回状态码 500,表示服务器内部错误    
public RestErrorResponse methodArgumentNotValidException(MethodArgumentNotValidException e) {  
  
        // 解析异常    
BindingResult bindingResult = e.getBindingResult();  
        // 存放错误信息    
List<String> errors = new ArrayList<>();  
        bindingResult.getFieldErrors().stream().forEach(item->{  
            errors.add(item.getDefaultMessage());  
        });  
        // 拼接错误信息    
String errMessage = StringUtils.join(errors, ",");  
  
        // 记录异常信息到日志中    
log.error("【系统异常】{}",e.getMessage(),errMessage);  
        // 返回通用的错误响应    
RestErrorResponse restErrorResponse = new RestErrorResponse(errMessage);  
        return restErrorResponse;  
    }  
}

中州养老异常处理

  1. BaseException.java:

    • 这个类定义了一个基本的异常类,其中包含了模块、错误码、错误消息等信息。
    • 通过构造方法可以设置异常的模块、错误码、参数和默认消息。
    • 用于自定义具体业务异常的基类,在具体业务异常类中可以继承该类并设置具体的异常信息。
  2. ProjectException.java:

    • 这个类是自定义异常类,继承了RuntimeException,用于表示项目中发生的异常情况。
    • 包含了错误编码、提示信息等属性,以及对应的接口信息。
    • 可以通过不同的构造方法设置异常的错误码和提示信息,方便在项目中抛出自定义异常并传递相关信息。
  3. GlobalExceptionHandler.java:

    • 这个类是全局异常处理器,使用@RestControllerAdvice注解标记为全局异常处理类。
    • 包含了多个异常处理方法,针对不同类型的异常进行处理并返回相应的响应数据。
    • 通过各个@ExceptionHandler方法捕获特定类型的异常,并返回对应的响应数据,例如设置HTTP响应状态码、错误消息等。

1、自定义异常`BaseException

package com.zzyl.exception;  
  
  
import lombok.Data;  
  
/**  
 * BaseException * @author itheima  
 **/@Data  
public class BaseException extends RuntimeException {  
    private static final long serialVersionUID = 1L;  
  
    /**  
     * 所属模块  
     */  
    private String module;  
  
    /**  
     * 错误码  
     */  
    private String code;  
  
    /**  
     * 错误码对应的参数  
     */  
    private Object[] args;  
  
    /**  
     * 错误消息  
     */  
    private String defaultMessage;  
  
    public BaseException(String module, String code, Object[] args, String defaultMessage) {  
        this.module = module;  
        this.code = code;  
        this.args = args;  
        this.defaultMessage = defaultMessage;  
    }  
  
    public BaseException(String module, String code, Object[] args) {  
        this(module, code, args, null);  
    }  
  
    public BaseException(String module, String defaultMessage) {  
        this(module, null, null, defaultMessage);  
    }  
  
    public BaseException(String code, Object[] args) {  
        this(null, code, args, null);  
    }  
  
    public BaseException(String defaultMessage) {  
        this(null, "500", null, defaultMessage);  
    }  
  
  
    public String getModule() {  
        return module;  
    }  
  
    public String getCode() {  
        return code;  
    }  
  
    public Object[] getArgs() {  
        return args;  
    }  
  
    public String getDefaultMessage() {  
        return defaultMessage;  
    }  
}
1-1、使用示例

构造方法
 BaseException(String module, String code, Object[] args, String defaultMessage):

throw new BaseException("ModuleA", "401", new Object[]{"arg1", "arg2"}, "Default error message");

构造方法 
BaseException(String module, String code, Object[] args):

throw new BaseException("ModuleB", "404", new Object[]{"arg3", "arg4"});

构造方法
 BaseException(String module, String defaultMessage):

throw new BaseException("ModuleC", "Custom error message");

构造方法
 BaseException(String code, Object[] args):

throw new BaseException("500", new Object[]{"arg5"});

构造方法
 BaseException(String defaultMessage):

throw new BaseException("Default error message");`

以上示例展示了不同构造方法在创建BaseException对象时的用法,根据传入的参数设置了模块、错误码、参数以及默认消息等属性。这些方法可以根据具体业务场景中异常的情况来灵活使用,以便准确传达异常信息并进行处理。

2、自定义异常`ProjectException

package com.zzyl.exception;  
  
import com.zzyl.base.IBasicEnum;  
  
/**  
 * 自定义异常  
 */  
public class ProjectException extends RuntimeException {  
  
    //错误编码  
    private int code;  
  
    //提示信息  
    private String message;  
  
    //异常接口  
    private IBasicEnum basicEnumIntface;  
  
    public ProjectException() {  
  
    }  
    public ProjectException(int code, String message) {  
        this.code = code;  
        this.message = message;  
    }  
  
    public ProjectException(IBasicEnum errorCode) {  
        setBasicMsg(errorCode);  
    }  
  
  
    public ProjectException(IBasicEnum errorCode, String throwMsg) {  
        super(throwMsg);  
        setBasicMsg(errorCode);  
    }  
  
    public ProjectException(IBasicEnum errorCode, Throwable throwable) {  
        super(throwable);  
        setBasicMsg(errorCode);  
    }  
  
  
    private void setBasicMsg(IBasicEnum basicEnumIntface) {  
        this.code = basicEnumIntface.getCode();  
        this.message = basicEnumIntface.getMsg();  
        this.basicEnumIntface = basicEnumIntface;  
    }  
  
    public int getCode() {  
        return code;  
    }  
  
    public void setCode(int code) {  
        this.code = code;  
    }  
  
    @Override  
    public String getMessage() {  
        return message;  
    }  
  
    public void setMessage(String message) {  
        this.message = message;  
    }  
  
    public IBasicEnum getBasicEnumIntface() {  
        return basicEnumIntface;  
    }  
  
    public void setBasicEnumIntface(IBasicEnum basicEnumIntface) {  
        this.basicEnumIntface = basicEnumIntface;  
    }  
  
    @Override  
    public String toString() {  
        return "ProjectException{" +  
                "code='" + code + '\'' +  
                ", message='" + message + '\'' +  
                ", basicEnumIntface=" + basicEnumIntface +  
                '}';  
    }  
}

3、全局异常`GlobalExceptionHandler

package com.zzyl.exception;  
  
import cn.hutool.core.exceptions.ExceptionUtil;  
import cn.hutool.core.map.MapUtil;  
import cn.hutool.core.util.ObjectUtil;  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.dao.DuplicateKeyException;  
import org.springframework.http.HttpStatus;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.ExceptionHandler;  
import org.springframework.web.bind.annotation.RestControllerAdvice;  
import org.springframework.web.multipart.MaxUploadSizeExceededException;  
  
import java.io.FileNotFoundException;  
import java.nio.file.AccessDeniedException;  
  
@RestControllerAdvice  
@Slf4j  
public class GlobalExceptionHandler {  
  
    /**  
     * 处理自定义异常BaseException。  
     * 返回自定义异常中的错误代码和错误消息。  
     *  
     * @param exception 自定义异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(BaseException.class)  
    public ResponseEntity<Object> handleBaseException(BaseException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("自定义异常处理 -> ", exception);  
        }  
        return ResponseEntity.ok(MapUtil.<String, Object>builder()  
                .put("code", exception.getCode())  
                .put("msg", exception.getDefaultMessage())  
                .build());  
    }  
  
    /**  
     * 处理文件上传超过最大限制异常。  
     * 返回HTTP响应状态码500,包含错误代码和错误消息。  
     *  
     * @param exception 文件上传异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(MaxUploadSizeExceededException.class)  
    public ResponseEntity<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("文件上传超过最大限制异常 -> ", exception);  
        }  
  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.INTERNAL_SERVER_ERROR.value())  
                        .put("msg", "上传图片大小不能超过5M,格式需为jpg、png、gif")  
                        .build());  
    }  
  
    /**  
     * 处理其他未知异常。  
     * 返回HTTP响应状态码500,包含错误代码和异常堆栈信息。  
     *  
     * @param exception 未知异常  
     * @return 响应数据,包含错误代码和异常堆栈信息  
     */  
    @ExceptionHandler(Exception.class)  
    public ResponseEntity<Object> handleUnknownException(Exception exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("其他未知异常 -> ", exception);  
        }  
  
  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.INTERNAL_SERVER_ERROR.value())  
                        .put("msg", ExceptionUtil.stacktraceToString(exception))  
                        .build());  
    }  
  
    /**  
     * 处理FileNotFoundException异常。  
     * 返回HTTP响应状态码400,包含错误代码和错误消息。  
     *  
     * @param exception 文件未找到异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(FileNotFoundException.class)  
    public ResponseEntity<Object> handleFileNotFoundException(FileNotFoundException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("文件不存在 -> ", exception);  
        }  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.BAD_REQUEST.value())  
                        .put("msg", exception.getMessage())  
                        .build());  
    }  
  
    /**  
     * 处理没有权限访问接口异常。  
     * 返回HTTP响应状态码401,包含错误代码和错误消息。  
     *  
     * @param exception 权限访问异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(AccessDeniedException.class)  
    public ResponseEntity<Object> handleAccessDeniedException(AccessDeniedException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("没有权限访问接口异常 -> ", exception);  
        }  
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.UNAUTHORIZED.value())  
                        .put("msg", "没有权限访问接口")  
                        .build());  
    }  
  
    /**  
     * 处理运行时异常。  
     * 返回HTTP响应状态码500,包含错误代码和错误消息。  
     *  
     * @param exception 运行时异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(RuntimeException.class)  
    public ResponseEntity<Object> handleRuntimeException(RuntimeException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("其他未知异常 -> ", exception);  
        }  
  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.INTERNAL_SERVER_ERROR.value())  
                        .put("msg", exception.getMessage())  
                        .build());  
    }  
  
    /**  
     * 处理key重复异常。  
     * 返回HTTP响应状态码200,包含错误代码和错误消息。  
     *  
     * @param exception key重复异常  
     * @return 响应数据,包含错误代码和错误消息  
     */  
    @ExceptionHandler(DuplicateKeyException.class)  
    public ResponseEntity<Object> handleDuplicateKeyException(DuplicateKeyException exception) {  
        exception.printStackTrace();  
        if (ObjectUtil.isNotEmpty(exception.getCause())) {  
            log.error("其他未知异常 -> ", exception);  
        }  
        return ResponseEntity.status(HttpStatus.OK)  
                .body(MapUtil.<String, Object>builder()  
                        .put("code", HttpStatus.INTERNAL_SERVER_ERROR.value())  
                        .put("msg", "操作失败,数据重复")  
                        .build());  
    }  
}
  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在基于SpringBoot + SpringCloud + Vue项目的开发中,如果需要集成RS232串口通信功能,可以按照以下步骤进行: 1. 引用中提到的【springBoot + springCloud + vue 项目】一,搭建后端环境。确保项目已经成功运行,并且相关依赖已添加。 2. 引用中提到的【springBoot + springCloud + vue 项目】二,详细了解后端框架。确保你已经了解了SpringBoot的基本原理和使用方法。 3. 在项目中添加RS232串口通信的相关依赖。可以使用Java的串口通信库,如RXTX或JSerialComm。这些库提供了高级的API来处理串口通信的细节。 4. 在项目中创建一个专门的类或模块来处理RS232通信。这个类应该包含打开串口、读取数据、发送数据等方法。可以参考串口通信库的文档或示例代码来实现这些功能。 5. 根据项目的需求,在合适的地方调用串口通信类的方法来实现与RS232设备的通信。可以根据具体的需求来编写相关的业务逻辑。 6. 测试串口通信功能。可以使用模拟器或真实的RS232设备来测试通信是否正常。确保能够正常收发数据。 7. 完善异常处理机制。在串口通信过程中,可能会出现各种异常情况,如串口被占用、通信超时等。要确保程序能够正确处理这些异常情况,并给出相应的提示或处理方法。 8. 引用中提到的【springBoot + springCloud + vue 项目】三,项目部署。将项目部署到服务器上,并对RS232串口通信进行最终的测试和验证。 总结起来,集成RS232串口通信功能需要在SpringBoot + SpringCloud + Vue项目的基础上,添加相关依赖,编写串口通信类,并在合适的地方调用这个类的方法来实现与RS232设备的通信。在实际开发中,还需要考虑异常处理和部署等问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springboot springcloud 父项目pom工程创建pom文件](https://blog.csdn.net/weixin_39933484/article/details/111954859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值