springboot 快速开发的定制补充

增强 SpringBoot 快速开发工具

项目地址:https://gitee.com/sanri/web-ui
优点:这是一个 web 通用配置的组件,即插即用,可用于新项目或私活。是对 SpringBoot 快速开发的一种补充,它内置了大量的配置来简化开发,遵循约定高于配置原则。

它解决的问题:

  • 固定了输入输出格式
  • 对于 Controller 中的返回不用关心包装类型,返回你所需要的类型就可以了,对于 insert 单表操作可以直接返回 void
  • 如果项目中出现业务操作不符合或调用第三方出错,可使用异常或断言抛出,我们将拦截成统一格式返回
  • 自带参数空格过滤功能,还可以定义特殊字符和谐
  • 支持校验器,已经帮你设置好了两个 group
  • 支持大文件分片上传

发现BUG可以提Issue,可以给我发邮件,可以加我QQ,可以进9420技术群讨论.

作者QQ: 2441719087

作者邮箱: [email protected]

9420 技术交流群: 645576465

作者微信:sanri1993-
在这里插入图片描述

项目功能

我新开的一个项目,总结了以往 4 年的开发经验所得,它具有的功能有

  • 固定了输入输出格式

    // 普通输出格式
    @Data
    public class ResponseDto<T> implements Serializable {
        // 0 字符串表示成功,否则失败
        private String code = "0";
        private String message;
        private T data;
    }
    // 分页输出格式,是包裹在普通输出格式中的,PageResponseDto 做为 data 属性
    @Data
    public class PageResponseDto<T> {
        private List<T> rows;
        private Integer total;
    }
    
    // 分页输入格式 
    @Setter
    public class PageParam {
      private String pageNo;
      private String pageSize;
    }
  • 对于 Controller 中的返回不用关心包装类型,返回你所需要的类型就可以了,对于 insert 单表操作可以直接返回 void

    示例一:

    @PostMapping("/insertUser")
    public void insertUser(User user){
        xxxService.insert(user);
    }

    它将会返回这样的数据结构

    {
        "code":"0",
        "message":"ok",
        "data":null
    }

    示例二:

    @GetMapping("/queryUserById")
    public User queryUserById(Integer userId){
        xxxService.queryUserById(userId);
    }

    它将会返回这样的数据结构

    {
        "code":"0",
        "message":"ok",
        "data":{
            "userId":1,
            "username":"9420"
        }
    }

    示例三:

    对于分页数据的处理

    @GetMapping("/queryUserPage")
    public PageResponseDto<User> pageQuery(PageParam pageParam,Map<String,String> queryParams){
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());
        Page page = (Page) xxxService.pageQuery(queryParams);
        List result = page.getResult();
        long total = page.getTotal();
        return new PageResponseDto(result,total);
    }

    它将会返回这样的数据结构

    {
        "code":"0",
        "message":"ok",
        "data":{
            "total":100,
            "rows":[{...},{...}]
        }
    }
  • 如果项目中出现业务操作不符合或调用第三方出错,可使用异常抛出,我们将拦截成统一格式返回

    示例一:

    if(业务条件不满足){
        throw BusinessException.create("业务提示信息");
    }

    它将会返回这样的数据结构,code 是随机生成的

    {
        "code":"234234",
        "message":"业务提示信息",
        "data":null
    }

    示例二:

    自定义 code 示例方法一

    if(业务条件不满足){
        throw BusinessException.create("E007","业务提示信息");
    }

    它将会返回这样的数据结构

    {
        "code":"E007",
        "message":"业务提示信息",
        "data":null
    }

    示例三:

    自定义 code 示例方法二

    // 配置异常代码 
    public enum  SystemMessage implements ExceptionCause<BusinessException> {
        SIGN_ERROR(4005,"签名错误,你的签名串为 [%s]"),;
        ResponseDto responseDto = new ResponseDto();
    
        private SystemMessage(int returnCode,String message){
            responseDto.setCode(returnCode+"");
            responseDto.setMessage(message);
        }
    
        public BusinessException exception(Object...args) {
            return BusinessException.create(this,args);
        }
    }

    使用异常

    if(业务条件不满足){
        throw SystemMessage.SIGN_ERROR.exception("签名串");
    }

    它将会返回这样的数据结构

    {
        "code":"4005",
        "message":"签名错误,你的签名串为 [签名串]",
        "data":null
    }
  • 你以为它就这么点能耐吗,它还自带参数空格过滤功能,还可以定义特殊字符和谐

    你只需要注入一个处理器,它就能工作,注入方式如下

    @Bean("paramHandler")
    public Function paramHandler(){
        return param -> param.replace("<","《");
    }
  • 自带了日期转化(输入)功能,可以支持的日期格式有

    final String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S"};

    现在是固定这三种格式 ,后面会放开让使用者自己配置

  • 支持校验器,已经帮你设置好了两个 group ,直接使用即可

    public interface Insert {
    }
    public interface Update {
    }

使用说明

引入包或下载 jar 包文件

<dependency>
    <groupId>com.sanri.web</groupId>
    <artifactId>web-ui</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

开启快速开发

@EnableWebUI 

一点小推广

创作不易,希望可以支持下我的开源软件,及我的小工具,欢迎来 gitee 点星,fork ,提 bug 。

Excel 通用导入导出,支持 Excel 公式
博客地址:https://blog.csdn.net/sanri1993/article/details/100601578
gitee:https://gitee.com/sanri/sanri-excel-poi

使用模板代码 ,从数据库生成代码 ,及一些项目中经常可以用到的小工具
博客地址:https://blog.csdn.net/sanri1993/article/details/98664034
gitee:https://gitee.com/sanri/sanri-tools-maven

猜你喜欢

转载自www.cnblogs.com/sanri1993/p/11723163.html