CIM项目

https://github.com/CryBecase/im-server

异常处理

  1. BaseException

    public class BaseException extends RuntimeException {
     /* 模块 */
     private String module;
     /* 错误信息 */
     private String message;
     /* 参数集 */
     private Object[] args;
    
     public BaseException(String module, String message) {
         this(module, message, null);
     }
    
     public BaseException(String module, String message, Object[] args) {
         this.module = module;
         this.message = message;
         this.args = args;
     }
     // getter setter...
    }

    若 Chat 模块出现了问题,则有 ChatException 类来继承此类抛出异常。

  2. GlobalExceptionHandler

    @RestControllerAdvice
    public class GlobalExceptionHandler {
     /**
      * 运行时异常
      */
     @ExceptionHandler(RuntimeException.class)
     public AjaxResult runtimeExceptionHandler(RuntimeException e) {
         return AjaxResult.error(e.getMessage());
     }
    
     /**
      * 异常
      */
     @ExceptionHandler({Exception.class})
     public AjaxResult exceptionHandler(Exception e) {
         return AjaxResult.error(e.getMessage());
     }
    
     @ExceptionHandler({NullPointerException.class})
     public AjaxResult nullPointerExceptionHandler(NullPointerException e) {
         return AjaxResult.error(ExceptionMessage.NULL_POINTER_EXCEPTION);
     }
    }

    此类用来监听 Controller、Interceptor 中抛出的异常并捕获,再根据类型进行处理。

Swagger

  1. 配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
     @Bean
     public Docket createRestApi() {
         return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                 .select()
                 .apis(RequestHandlerSelectors.basePackage("com.xiaoci.im.controller"))
                 .paths(PathSelectors.any())
                 .build();
     }
    
     private ApiInfo apiInfo() {
         return new ApiInfoBuilder().title("CIM")
                 .description("Swagger API 帮助您测试")
                 .contact(new Contact("Xiao Ci", "", "1046060018@qq.com"))
                 .version("0.0.1-SNAPSHOT")
                 .build();
     }
    }
  2. 使用

    @RestController
    @Api(value = "用户管理")
    public class UserController {
    
     @PostMapping("/register")
     @ApiOperation(value = "注册")
     public AjaxResult register(@RequestBody RegisterDTO registerDTO) {
         // ...
         return AjaxResult.success();
     }
    }

常用注解:

  • @Api()用于类;
    表示标识这个类是swagger的资源
  • @ApiOperation()用于方法;
    表示一个http请求的操作
  • @ApiParam()用于方法,参数,字段说明;
    表示对参数的添加元数据(说明或是否必填等)
  • @ApiModel()用于类
    表示对类进行说明,用于参数用实体类接收
  • @ApiModelProperty()用于方法,字段
    表示对model属性的说明或者数据操作更改
  • @ApiIgnore()用于类,方法,方法参数
    表示这个方法或者类被忽略
  • @ApiImplicitParam() 用于方法
    表示单独的请求参数
  • @ApiImplicitParams() 用于方法
    包含多个 @ApiImplicitParam,使用@RequestBody的参数可以不写

MBG(Mybatis Generator)

resources 包中添加 generatorConfig.xml 对 MBG 进行配置
在maven中使用此命令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate,就按照配置文件的规则,生成对应的Entity和Mapper。
注意: 在连接MySQL8.0的时候,一定要在 url 中加上 nullCatalogMeansCurrent=true,否则在按照表名寻找表的过程中会匹配所有数据库中的表。

druid(阿里的数据库连接池)

druid

websocket

// TODO...