Jackson @JsonView examples

摘要: In Jackson, we can use @JsonView to limit or control fields display for different users.

In Jackson, we can use @JsonView to limit or control fields display for different users.

A POJO for testing.

Staff.java
package com.mkyong;
public class Staff {
    private String name;
    private int age;
    private String[] position;
    private List<String> skills;
    private Map<String, BigDecimal> salary;
	// getters , setters , boring stuff

P.S Tested with Jackson 2.9.8

1. Views

A standard Java class to define 3 views: normal, manager and hr.

CompanyViews.java
package com.mkyong;
public class CompanyViews {
    public static class Normal{};
    public static class Manager extends Normal{};
    public static class HR extends Normal{};

2. Json View

Puts @JsonView on field level to limit fields display for different views.

  • Normal – display name and age.
  • Manager – display name, age, position and skills
  • HR – display name, age, salary and position

P.S Manager has no right to view the salary field, and HR doesn’t care what skills you have :)

Staff.java
package com.mkyong;
import com.fasterxml.jackson.annotation.JsonView;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Staff {
    @JsonView(CompanyViews.Normal.class)
    private String name;
    @JsonView(CompanyViews.Normal.class)
    private int age;
    // two views
    @JsonView({CompanyViews.HR.class, CompanyViews.Manager.class})
    private String[] position;
    @JsonView(CompanyViews.Manager.class)
    private List<String> skills;
    @JsonView(CompanyViews.HR.class)
    private Map<String, BigDecimal> salary;

3. Jackson – Enable the @JsonView

3.1 Below example, show you how to enable the JsonView with mapper.writerWithView()

JacksonExample.java
package com.mkyong;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class JacksonExample {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        Staff staff = createStaff();
        try {
            // to enable pretty print
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            // normal
            String normalView = mapper.writerWithView(CompanyViews.Normal.class).writeValueAsString(staff);
            System.out.format("Normal views\n%s\n", normalView);
            // manager
            String managerView = mapper.writerWithView(CompanyViews.Manager.class).writeValueAsString(staff);
            System.out.format("Manager views\n%s\n", managerView);
            // hr
            String hrView = mapper.writerWithView(CompanyViews.HR.class).writeValueAsString(staff);
            System.out.format("HR views\n%s\n", hrView);
        } catch (IOException e) {
            e.printStackTrace();
    private static Staff createStaff() {
        Staff staff = new Staff();
        staff.setName("mkyong");
        staff.setAge(38);
        staff.setPosition(new String[]{"Founder", "CTO", "Writer"});
        Map<String, BigDecimal> salary = new HashMap() {{
            put("2010", new BigDecimal(10000));
            put("2012", new BigDecimal(12000));
            put("2018", new BigDecimal(14000));
        }};
        staff.setSalary(salary);
        staff.setSkills(Arrays.asList("java", "python", "node", "kotlin"));
        return staff;

Output

Normal views
  "name" : "mkyong",
  "age" : 38
Manager views
  "name" : "mkyong",
  "age" : 38,
  "position" : [ "Founder", "CTO", "Writer" ],
  "skills" : [ "java", "python", "node", "kotlin" ]
HR views
  "name" : "mkyong",
  "age" : 38,
  "position" : [ "Founder", "CTO", "Writer" ],
  "salary" : {
    "2018" : 14000,
    "2012" : 12000,
    "2010" : 10000

上一篇: Java Check if a String is empty or null
下一篇: Jackson How to parse JSON
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号