zoukankan      html  css  js  c++  java
  • 第3章 springboot接口返回json 3-2 Jackson的基本演绎法

        @JsonIgnore
        private String password;

        @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a",locale="zh", timezone="GMT+8")
        private Date birthday;
        @JsonInclude(Include.NON_NULL)
        private String desc;

     /imooc-springboot-starter/src/main/java/com/imooc/controller/UserController.java

    package com.imooc.controller;
    
    import java.util.Date;
    
    //import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    //import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.imooc.pojo.LeeJSONResult;
    import com.imooc.pojo.User;
    
    //@Controller
    @RestController // @RestControler = @Controller + @ResponseBody
    @RequestMapping("/user")
    public class UserController {
        
        //@RequestMapping("/hello")
        @RequestMapping("/getUser")
        //@ResponseBody
        public User hello() {
          //public User getUser() {    
            User u = new User();
            u.setName("imooc");
            u.setAge(18);
            u.setBirthday(new Date());
            u.setPassword("imooc");
            //u.setDesc(null);
            u.setDesc("hello imooc~~");
            
            return u;
            
        }
        @RequestMapping("/getUserJson")
        //@ResponseBody
        public LeeJSONResult hello1() {
          //public LeeJsonResult getUserJson() {    
            User u = new User();
            u.setName("imooc");
            u.setAge(18);
            u.setBirthday(new Date());
            u.setPassword("imooc");
            //u.setDesc(null);
            u.setDesc("hello imooc~~");
            
            return LeeJSONResult.ok(u);
            
        }
    }

    /imooc-springboot-starter/src/main/java/com/imooc/pojo/User.java

    package com.imooc.pojo;
    
    import java.util.Date;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    @JsonIgnoreProperties
    public class User {
        
        private String name;
        @JsonIgnore
        private String password;
        private Integer age;
        @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a",locale="zh", timezone="GMT+8")
        private Date birthday;
        @JsonInclude(Include.NON_NULL)
        private String desc;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        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;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
  • 相关阅读:
    【Leetcode】Unique Binary Search Trees
    linux C函数之access函数的用法
    Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析
    【Leetcod】Unique Binary Search Trees II
    KVM客户机使用主机USB设备
    运行Maven是报错:No goals have been specified for this build
    SQL2008R2 express版本不支持维护计划
    已超过了锁请求超时时段的原因
    Oracle免客户端InstantClient安装使用
    将存储过程的返回值赋给变量
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/9864476.html
Copyright © 2011-2022 走看看