• 开发笔记


    一、工具类(服务器返回数据) 2019/1/11

    //保证序列化JSON的时候,如果对象为null,key也会消失
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    public class ServerResponse<T> implements Serializable {
        private int status;
        private String msg;
        private T data;
    
        private ServerResponse(int status) {
            this.status = status;
        }
    
        private ServerResponse(int status, String msg, T data) {
            this.status = status;
            this.msg = msg;
            this.data = data;
        }
    
        private ServerResponse(int status, String msg) {
            this.status = status;
            this.msg = msg;
        }
    
        private ServerResponse(int status, T data) {
            this.status = status;
            this.data = data;
        }
    
        /**
         * 如果返回0,代表响应成功
         * @return
         */
        @JsonIgnore
        public boolean isSuccess(){
            return this.status == ResponseCode.SUCCESS.getCode();
        }
    
        public int getStatus() {
            return status;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public T getData() {
            return data;
        }
    
        public static <T> ServerResponse<T> createBySuccess(){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
        }
    
        public static <T> ServerResponse<T> createBySuccessMsg(String msg){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
        }
    
        public static <T> ServerResponse<T> createBySuccess(T data){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
        }
    
        public static <T> ServerResponse<T> createBySuccess(String msg, T data){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
        }
    
        public static <T> ServerResponse<T> createByError(){
            return new ServerResponse<T>(ResponseCode.ERROR.getCode()
                    ,ResponseCode.ERROR.getDesc());
        }
    
        public static <T> ServerResponse<T> createByError(String errorMessage){
            return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
        }
    
        public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
            return new ServerResponse<T>(errorCode,errorMessage);
        }
    }

    使用:

    try {
                Data data = service.method(param);
                return ServerResponse.createBySuccess(data);
            }catch (Exception e){
                LOGGER.error(ExceptionUtil.getMessage(e));
                return ServerResponse.createByError("数据获取失败!");
            }

    二、工具类(通过spring aop 处理所有@RequestMapping注解方法的异常) 2019/1/11

    package com.data.ops;
    
    import com.alibaba.fastjson.JSON;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @Aspect
    @Component
    public class WebExceptionAspect {
        private static final Logger logger = LoggerFactory.getLogger(WebExceptionAspect.class);
    
        //凡是注解了RequestMapping的方法都被拦截
        @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
        private void webPointcut() {
        }
    
        /**
         * 拦截web层异常,记录异常日志,并返回友好信息到前端 目前只拦截Exception,是否要拦截Error需再做考虑     
       *
       *
    @param e
       * 异常对象
    */ @AfterThrowing(pointcut = "webPointcut()", throwing = "e") public void handleThrowing(Exception e) { e.printStackTrace(); logger.error("发现异常!" + e.getMessage()); logger.error(JSON.toJSONString(e.getStackTrace())); //这里输入友好性信息 writeContent("出现异常"); } /**
       * 将内容输出到浏览器
       *
       *
    @param content
       * 输出内容
       */ private void writeContent(String content) { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "text/plain;charset=UTF-8"); response.setHeader("icop-content-type", "exception"); PrintWriter writer = null; try { writer = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } writer.print(content); writer.flush(); writer.close(); }}

     开发中问题:

    1、spring boot项目请求不到图片资源

    最快解决方案,添加Thymeleaf 模板引擎依赖。

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    2、IDEA按键错乱问题(IDEA VIM编辑模式)

    勾掉 菜单栏-Tools-Vim Emulator(Ctrl+Alt+V)

     css小技巧(2019/5/30)

    - id名和class名是区分大小写的 
    - 选择器中,只有标签选择器不区分大小写 
    - 属性名和属性值是不区分大小写的

    2019/6/6

    生成随机6为字符串

    String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
    

     2019/11/13

    完整发布,设置版本号,不需要让用户清理缓存

  • 相关阅读:
    Linux内核的整体框架
    Unix环境高级编程_文件和目录
    Unix环境高级编程_文件I/O
    u-boot启动的第二阶段
    linux基础之vi编辑器设置文件模板
    ARM linux开发之安装配置tftp
    STM32笔试题笔记
    linux基础之find命令常用用法
    ARM linux开发之根文件系统
    ARM linux开发之linux内核启动简介
  • 原文地址:https://www.cnblogs.com/SongG-blogs/p/10254169.html
Copyright © 2020-2023  润新知