zoukankan      html  css  js  c++  java
  • 框架 Spring Boot 技术入门到整合 3-2 Jackson的基本演绎法

    0    视频地址
    1    课程内容
    1.1  @JsonIgnoreProperties({"a","b"})  

    类上加此注解,json中忽略相应元素,不上传

    1.2  @JsonIgnore

    属性上加此注解,json中忽略相应元素,不上传

     

    1.3  @JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss a",locale = "zh",timezone = "GMT+8")//日期格式

    日期可以转格式

    1.4  @JsonInclude(JsonInclude.Include.NON_NULL)//为空不显示 JsonInclude.Include.ALWAYS 总显示
    为空时不显示
    2    课程demo
    2.1  demo1

    bean类:

    package com.example.demo.bean;
    
    import com.fasterxml.jackson.annotation.*;
    
    import java.util.Date;
    
    /**
     * User
     *
     * @author 魏豆豆
     * @date 2020/11/15
     */
    @JsonIgnoreProperties({"userAge","passWord"})
    public class User {
        @JsonIgnore
        private String userID;
        private String userName;
        private int userAge;
        private String passWord;
        @JsonFormat(pattern = "yyyy/MM/dd hh:mm:ss a",locale = "zh",timezone = "GMT+8")//日期格式
        private Date birthDay;
        @JsonInclude(JsonInclude.Include.NON_NULL)//为空不显示 JsonInclude.Include.ALWAYS 总显示
        private String desc;
    
        public String getUserID() {
            return userID;
        }
    
        public void setUserID(String userID) {
            this.userID = userID;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public int getUserAge() {
            return userAge;
        }
    
        public void setUserAge(int userAge) {
            this.userAge = userAge;
        }
    
        public String getPassWord() {
            return passWord;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    
        public Date getBirthDay() {
            return birthDay;
        }
    
        public void setBirthDay(Date birthDay) {
            this.birthDay = birthDay;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

    Controller类:

    package com.example.demo.son.demob;
    
    import com.example.demo.bean.User;
    import com.example.demo.tools.JSONResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    
    /**
     * UserControllerA
     * 目的:返回json对象
     * @author 魏豆豆
     * @date 2020/11/15
     */
    @RestController
    @RequestMapping("/user")
    public class UserControllerB {
    
        @RequestMapping("/getUserJson")
        public JSONResult getUser(){
            User u = new User();
            u.setUserID("20201115001");
            u.setUserName("大黄");
            u.setPassWord("dahuang");
            u.setBirthDay(new Date());
            u.setUserAge(18);
            return JSONResult.errorMap(u);
        }
    }

    组装类:

    package com.example.demo.tools;
    
    import java.util.List;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /**
     *
     * @Title: LeeJSONResult.java
     * @Package com.lee.utils
     * @Description: 自定义响应数据结构
     *                 这个类是提供给门户,ios,安卓,微信商城用的
     *                 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list)
     *                 其他自行处理
     *                 200:表示成功
     *                 500:表示错误,错误信息在msg字段中
     *                 501:bean验证错误,不管多少个错误都以map形式返回
     *                 502:拦截器拦截到用户token出错
     *                 555:异常抛出信息
     * Copyright: Copyright (c) 2016
     * Company:Nathan.Lee.Salvatore
     *
     * @author leechenxiang
     * @date 2016年4月22日 下午8:33:36
     * @version V1.0
     */
    public class JSONResult {
    
        // 定义jackson对象
        private static final ObjectMapper MAPPER = new ObjectMapper();
    
        // 响应业务状态
        private Integer status;
    
        // 响应消息
        private String msg;
    
        // 响应中的数据
        private Object data;
    
        private String ok;    // 不使用
    
        public static JSONResult build(Integer status, String msg, Object data) {
            return new JSONResult(status, msg, data);
        }
    
        public static JSONResult ok(Object data) {
            return new JSONResult(data);
        }
    
        public static JSONResult ok() {
            return new JSONResult(null);
        }
    
        public static JSONResult errorMsg(String msg) {
            return new JSONResult(500, msg, null);
        }
    
        public static JSONResult errorMap(Object data) {
            return new JSONResult(501, "error", data);
        }
    
        public static JSONResult errorTokenMsg(String msg) {
            return new JSONResult(502, msg, null);
        }
    
        public static JSONResult errorException(String msg) {
            return new JSONResult(555, msg, null);
        }
    
        public JSONResult() {
    
        }
    
    //    public static LeeJSONResult build(Integer status, String msg) {
    //        return new LeeJSONResult(status, msg, null);
    //    }
    
        public JSONResult(Integer status, String msg, Object data) {
            this.status = status;
            this.msg = msg;
            this.data = data;
        }
    
        public JSONResult(Object data) {
            this.status = 200;
            this.msg = "OK";
            this.data = data;
        }
    
        public Boolean isOK() {
            return this.status == 200;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    
        /**
         *
         * @Description: 将json结果集转化为LeeJSONResult对象
         *                 需要转换的对象是一个类
         * @param jsonData
         * @param clazz
         * @return
         *
         * @author leechenxiang
         * @date 2016年4月22日 下午8:34:58
         */
        public static JSONResult formatToPojo(String jsonData, Class<?> clazz) {
            try {
                if (clazz == null) {
                    return MAPPER.readValue(jsonData, JSONResult.class);
                }
                JsonNode jsonNode = MAPPER.readTree(jsonData);
                JsonNode data = jsonNode.get("data");
                Object obj = null;
                if (clazz != null) {
                    if (data.isObject()) {
                        obj = MAPPER.readValue(data.traverse(), clazz);
                    } else if (data.isTextual()) {
                        obj = MAPPER.readValue(data.asText(), clazz);
                    }
                }
                return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         *
         * @Description: 没有object对象的转化
         * @param json
         * @return
         *
         * @author leechenxiang
         * @date 2016年4月22日 下午8:35:21
         */
        public static JSONResult format(String json) {
            try {
                return MAPPER.readValue(json, JSONResult.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         *
         * @Description: Object是集合转化
         *                 需要转换的对象是一个list
         * @param jsonData
         * @param clazz
         * @return
         *
         * @author leechenxiang
         * @date 2016年4月22日 下午8:35:31
         */
        public static JSONResult formatToList(String jsonData, Class<?> clazz) {
            try {
                JsonNode jsonNode = MAPPER.readTree(jsonData);
                JsonNode data = jsonNode.get("data");
                Object obj = null;
                if (data.isArray() && data.size() > 0) {
                    obj = MAPPER.readValue(data.traverse(),
                            MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
                }
                return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
            } catch (Exception e) {
                return null;
            }
        }
    
        public String getOk() {
            return ok;
        }
    
        public void setOk(String ok) {
            this.ok = ok;
        }
    
    }

    测试验证:

  • 相关阅读:
    [py]你真的了解多核处理器吗? 了解多线程
    [py]监控内存并出图
    [py]django强悍的数据库接口(QuerySet API)-增删改查
    【Unity Shaders】Transparency —— 透明的cutoff shader
    使用GDAL库中的RPC校正问题
    celery最佳实践
    Eclipse 快捷方式 指定 固定 workspace
    java 判断是否是周末
    如何设制 select 不可编辑 只读
    golang函数可变参数传递性能问题
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/13976247.html
Copyright © 2011-2022 走看看