Jackson序列化的视图模板@JsonView

实体类:

@Getter
@Setter
public class Role {
    @JsonView({RoleView.info.class, UserView.info.class})
    private String name;
    @JsonView({RoleView.info.class, UserView.info.class})
    private List<String> permission = new ArrayList<>(0);
    private Date createTime;
    private Boolean available = Boolean.FALSE;
}
@Getter
@Setter
public class User {
    @JsonView({UserView.login.class, UserView.info.class})
    private String name;
    @JsonIgnore
    private String password;
    @JsonView({UserView.info.class})
    private Role role;
    private Date createTime;
}

JsonView 类

public class RoleView {
    public static interface info{}
}
public class UserView {
    public static interface login{}
    public static interface info{}
}

Test类

public class UserTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserTest.class);

    @Test
    public void test() throws JsonProcessingException {
        User user = new User();
        Role role = new Role();
        List<String> list = Lists.newArrayList();
        list.add("user:list");
        list.add("user:add");
        list.add("user:delete");
        list.add("user:remove");
        list.add("user:update");
        role.setPermission(list);
        role.setName("admin");
        role.setCreateTime(new Date());

        user.setName("test001");
        user.setPassword("123456");
        user.setRole(role);

        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        om.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); //属性是默认时不进行序列化,例如当时List<Role> list = new ArrayList<>(0); 无权限时,可以进行自动忽略
//        om.setSerializationInclusion(JsonInclude.Include.NON_NULL); // null值不进行序列化
        om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // 集合为空、String的length()为0、时间为zero、其他null

        String json = om.writeValueAsString(user);
        LOGGER.debug("format json: {}", json);

//        om.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);//是一个双向开关,开启将输出没有JsonView注解的属性,false关闭将输出有JsonView注解的属性  

        json = om.writerWithView(UserView.info.class).writeValueAsString(user);
        LOGGER.debug("format userview.info json: {}", json);

        json = om.writerWithView(UserView.login.class).writeValueAsString(user);
        LOGGER.debug("format userview.login json: {}", json);
    }
}

输出:

13:26:23.097 [main] DEBUG cn.linuxcrypt.entity.UserTest - format json: {"name":"test001","role":{"name":"admin","permission":["user:list","user:add","user:delete","user:remove","user:update"],"createTime":"2016-08-04 13:26:22","available":false}}
13:26:23.104 [main] DEBUG cn.linuxcrypt.entity.UserTest - format userview.info json: {"name":"test001","role":{"name":"admin","permission":["user:list","user:add","user:delete","user:remove","user:update"],"createTime":"2016-08-04 13:26:22","available":false}}
13:26:23.105 [main] DEBUG cn.linuxcrypt.entity.UserTest - format userview.login json: {"name":"test001"}

转载于:https://my.oschina.net/wmsjhappy/blog/727172

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值