SpringBoot学习笔记四、json框架jackson返回结果处理

  • controller

package com.example.demo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.User;

@RestController
@RequestMapping("/other")
public class OtherController {
    Map<String,Object> result = new HashMap<String,Object>();

    /**
     * json序列化测试
     * 1、别名:@JsonProperty("name")
     * 2、忽略:@JsonIgnore
     * 3、非空:@JsonInclude(Include.NON_NULL)
     * 4、时间格式转换:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss", locale="zh", timezone="GMT+8")
     * @return
     */
    @GetMapping("testjson")
    public Object testjson(){
        result.clear();
        result.put("user1", new User("张三", "000001", "123456", 23, new Date()) );
        result.put("user2", new User("李四", "000001", "123456", null, null) );
        return result;
    }
}
 

  • User实体类: 

package com.example.demo.domain;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    
    @JsonProperty("name")
    private String username;
    
    @JsonIgnore
    private String userid;
    
    @JsonIgnore
    private String password;
    
    @JsonInclude(Include.NON_NULL)
    private Integer age;
    
    @JsonInclude(Include.NON_NULL)
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", locale="zh", timezone="GMT+8")
    private Date createTime;
    
    public User() {
        super();
    }
    
    public User(String username, String userid, String password, Integer age, Date createTime) {
        super();
        this.username = username;
        this.userid = userid;
        this.password = password;
        this.age = age;
        this.createTime = createTime;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值