【无标题】

分别测试一下jackson和fastjson的@JsonIgnore和@JSONField注解使用

1,序列化:对象转成json
2,反序列化:json转成对象


package com.msh.api.provide.dto.rulerepo;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 黑名单查询DTO
 *
 * @author ***
 * @date 2023/02/23
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class BlackListDTO {

    @Schema(description = "姓名")
    private String name;

    @Schema(description = "关联id")
    private String relationId;

    @Schema(description = "导出时多条使用关联id集合")
    private List<String> relationIdList;

    @Schema(description = "证件号码/手机号")
    private String idNoOrMobile;

    @Schema(description = "是否有风险 1是 0否")
    private String riskFlag;

    /**
     *
     */
    @JsonIgnore
    @Schema(description = "风险类别code集合(多选)")
    private List<String> riskCategoryList;

    /**
     * 序列化和反序列化都忽略此属性
     */
    @JSONField(serialize = false, deserialize = false)
    @Schema(description = "风险小类code集合(多选)")
    private List<String> riskSubCategoryList;

    @Schema(description = "风险类别code集合(多选)", hidden = true)
    private String riskCategoryListStr;

    @Schema(description = "风险小类code集合(多选)", hidden = true)
    private String riskSubCategoryListStr;


    @Schema(description = "当前页", required = true)
    private Integer page = 1;

    @Schema(description = "页大小", required = true)
    private Integer pageSize = 10;


    public String getRiskCategoryListStr() {
        if (CollectionUtils.isNotEmpty(riskCategoryList)) {
            return String.join(",", riskCategoryList);
        }
        return StringUtils.EMPTY;
    }

    public String getRiskSubCategoryListStr() {
        if (CollectionUtils.isNotEmpty(riskSubCategoryList)) {
            return String.join(",", riskSubCategoryList);
        }
        return StringUtils.EMPTY;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\"idNoOrMobile\":\"\",\"name\":\"\",\"page\":1,\"pageSize\":10,\"relationId\":\"\",\"relationIdList\":[\"1\",\"3\"],\"riskCategoryListStr\":\"\",\"riskFlag\":\"0\",\"riskSubCategoryList\":[\"1\",\"2\"],\"riskCategoryList\":[\"2\",\"5\"],\"riskSubCategoryListStr\":\"1,2\"}";
        BlackListDTO blackListDTOBuild = BlackListDTO.builder()
                .name("YuFu")
                .relationId("123")
                .relationIdList(Arrays.asList("1", "2", "3", "4"))
                .idNoOrMobile("17670747594")
                .riskFlag("1")
                .riskCategoryList(Arrays.asList("1", "2", "3", "4"))
                .riskSubCategoryList(Arrays.asList("5", "6", "7", "8"))
                .page(1)
                .pageSize(10)
                .build();
        //使用@JSONField(serialize = false, deserialize = false) json序列化不处理某数据
        //反序列化
        BlackListDTO jsonBlackListDTO = JSONObject.parseObject(json, BlackListDTO.class);
        System.out.println("blackListDTO:" + jsonBlackListDTO);
        //序列化
        String jsonString = JSONObject.toJSONString(blackListDTOBuild);
        System.out.println("jsonString:" + jsonString);

		
		 System.out.println("---------------------分割线------------------------------------");
		
        //jackson
        ObjectMapper mapper = new ObjectMapper();
        //反序列化
        BlackListDTO jacksonBlackListDTO = mapper.readValue(json, BlackListDTO.class);
        System.out.println("jacksonBlackListDTO:" + jacksonBlackListDTO);
        //序列化
        String jacksonString = mapper.writeValueAsString(blackListDTOBuild);
        System.out.println("jacksonString:" + jacksonString);
        
        
        /*
        * 结果返回
        * 
        * blackListDTO:BlackListDTO(name=, relationId=, relationIdList=[1, 3], idNoOrMobile=, riskFlag=0, riskCategoryList=[2, 5], riskSubCategoryList=null, riskCategoryListStr=2,5, riskSubCategoryListStr=, page=1, pageSize=10)
        jsonString:{"idNoOrMobile":"17670747594","name":"YuFu","page":1,"pageSize":10,"relationId":"123","relationIdList":["1","2","3","4"],"riskCategoryList":["1","2","3","4"],"riskCategoryListStr":"1,2,3,4","riskFlag":"1","riskSubCategoryListStr":"5,6,7,8"}
        jacksonBlackListDTO:BlackListDTO(name=, relationId=, relationIdList=[1, 3], idNoOrMobile=, riskFlag=0, riskCategoryList=null, riskSubCategoryList=[1, 2], riskCategoryListStr=, riskSubCategoryListStr=1,2, page=1, pageSize=10)
        jacksonString:{"name":"YuFu","relationId":"123","relationIdList":["1","2","3","4"],"idNoOrMobile":"17670747594","riskFlag":"1","riskSubCategoryList":["5","6","7","8"],"riskCategoryListStr":"1,2,3,4","riskSubCategoryListStr":"5,6,7,8","page":1,"pageSize":10}
        * 
        * */
    }
}

对了spring中默认是使用jackson的,如果@JsonIgnore不生效则使用@JSONField。
还有一些其它注解 如 @JsonIgnoreProperties 作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值