fastjson对接口参数的某个字段不打印输出,如文件的base64字符串

2024-10-09 10:57:49 浏览数 (3)

fastjson对接口参数的某个字段不打印输出,如文件的base64字符串

代码语言:javascript复制
package com.example.core.mydemo.json5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

/**
需要提供get set方法,如果使用@Data lombok 不生效(关键)
**
public class Item {
    private String id;
    @JSONField(serialize = false)  //fastjson 忽略字段打印
    private String rate;

    public Item() {
    }

    public Item(String id, String rate) {
        this.id = id;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return "Item{"  
                "id='"   id   '''  
                ", rate='"   rate   '''  
                '}';
    }

    /**
     * output  {"id":"111"}
     * @param args
     */
    public static void main(String[] args) {
        Item item = new Item();
        item.setId("111");
        item.setRate("56666");
        System.out.println(JSON.toJSONString(item));
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }
}

1 人点赞