zoukankan      html  css  js  c++  java
  • Spring Boot应用建议及脚手架工程

    规范详细说明

    1.遵循标准RESTful API

    2.异常采用枚举进行封装(业务内部异常往上抛,返回客户端需要将对应的异常转换为具体的状态的,这里介绍Spring提供的全局异常处理)

    package org.niugang.coding.advice;
    
    import lombok.extern.slf4j.Slf4j;
    import org.niugang.coding.enums.ExceptionEnum;
    import org.niugang.coding.exception.ServiceException;
    import org.niugang.coding.vo.ExceptionResult;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.validation.BindException;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    
    import javax.validation.ConstraintViolationException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 全部异常处理
     *
     * @author Created by niugang on 2018/12/26/12:19
     */
    @ControllerAdvice
    @Slf4j
    public class BasicExceptionHandler {
    
        /**
         * 具体业务层异常
         *
         * @param e 业务异常
         * @return ResponseEntity<ExceptionResult>
         */
        @ExceptionHandler(ServiceException.class)
        public ResponseEntity<ExceptionResult> handleException(ServiceException e) {
    
            /**
             * 响应的状态码,为枚举中定义的状态码
             */
            return ResponseEntity.status(e.getExceptionEnum().value())
                    .body(new ExceptionResult(e.getExceptionEnum()));
        }
    
    
        /**
         * 业务处理未知异常
         *
         * @param e 异常
         * @return ResponseEntity<ExceptionResult>
         */
        @ExceptionHandler(Exception.class)
        public ResponseEntity<?> exceptionResultResponseEntity(Exception e) {
            //所有参数异常
            //在对象上绑定校验如(UserDTO)
            if (e instanceof BindException || e instanceof MethodArgumentNotValidException || e instanceof IllegalArgumentException) {
                log.error("参数校验失败:{}", e);
                return ResponseEntity.status(ExceptionEnum.PARAMS_VALIDATE_FAIL.value())
                        .body(new ExceptionResult(ExceptionEnum.PARAMS_VALIDATE_FAIL));
            }
            //方法上参数校验失败
            if (e instanceof ConstraintViolationException) {
                ConstraintViolationException ex = (ConstraintViolationException) e;
                Map<String, Object> res = new HashMap<>(16);
                res.put("status", HttpStatus.BAD_REQUEST.value());
                res.put("message", ex.getMessage());
                res.put("timestamp", System.currentTimeMillis());
                return ResponseEntity.status(HttpStatus.BAD_REQUEST.value()).body(res);
    
            }
    
            log.error("服务器内部异常:{}", e);
            /*
             * 响应的状态码,为枚举中定义的状态码
             */
            return ResponseEntity.status(ExceptionEnum.BUSINESS_DEAL_FAIL.value())
                    .body(new ExceptionResult(ExceptionEnum.BUSINESS_DEAL_FAIL));
        }
    }
    
    • value 对应响应状态码

    • message 错误描述

    @NoArgsConstructor
    @AllArgsConstructor
    public enum ExceptionEnum {
        /**
         *
         */
        PARAMS_VALIDATE_FAIL(400, "'参数校验失败"),
        BUSINESS_DEAL_FAIL(500, "'业务处理失败");
        /**
         * 响应状态码
         */
        int value;
        /**
         * 响应描述
         */
        String message;
    
        public int value() {
            return this.value;
        }
    
        public String message() {
            return this.message;
        }
    }

    参数错误对应400状态码

    3.遵循严格的pojo,vo,dto(来自阿里java规范)

    4.建议提倡使用lomback(让你的代码,更加简洁,干净)

    5.对于日志记录采用@Slf4j

    以前可能是:

    private static final Logger logger = LoggerFactory.getLogger(MeetingInfoController.class);

    6.对于响应采用ResponseEntity,Spring已经封装好的,在一定程度上是能满足业务场景的

     @PostMapping
    public ResponseEntity<Void> save(@Valid @RequestBody  UserDTO userDTO) {
            userService.insert(userDTO);
            return ResponseEntity.ok().build();
        }

    7.RESTful API对应以下Spring请求注解

    • @GetMapping 查询

    • @PostMapping 新增

    • @PutMapping 修改

    • @DeleteMapping 删除

    8.进行必要的参数校验,新增参数校验,普通查询也有必要,如分页查询pageSize不做限制可能导致数据查询异常或慢查询

    9.建议直接只用框架自带封装好的API,如RedisTemplate,RabbitTemplate,KafkaTemplate(自己写的通用的可能某些地方考虑不全)

    10.使用注解事务@Transactional

    
    import org.springframework.transaction.annotation.Transactional; //spring的注解不是java注解

    源码地址:https://gitee.com/niugangxy/sprigboot-new-coding-standards

    微信公众号

                              
  • 相关阅读:
    vue自定义指令
    ZOJ Problem Set–2104 Let the Balloon Rise
    ZOJ Problem Set 3202 Secondprice Auction
    ZOJ Problem Set–1879 Jolly Jumpers
    ZOJ Problem Set–2405 Specialized FourDigit Numbers
    ZOJ Problem Set–1874 Primary Arithmetic
    ZOJ Problem Set–1970 All in All
    ZOJ Problem Set–1828 Fibonacci Numbers
    要怎么样调整状态呢
    ZOJ Problem Set–1951 Goldbach's Conjecture
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12186618.html
Copyright © 2011-2022 走看看