CIM项目 https://github.com/CryBecase/im-server 异常处理 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 类来继承此类抛出异常。  GlobalExceptionHandler @RestControllerAdvicepublic 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 配置类 @Configuration@EnableSwagger2public 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(); }}  使用 @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(阿里的数据库连接池) websocket // TODO...
点赞 0
评论 0
全部评论

相关推荐

07-28 16:15
门头沟学院 Java
点赞 评论 收藏
分享
07-07 17:06
已编辑
深圳技术大学 golang
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务