杰克逊映射器不映射所有字段

时间:2017-05-19 06:39:58

标签: java spring-boot jackson

我使用jackson将我的POJO数组转换为json,但它没有在输出中给出一个特定的字段,而gson.toJson给出了输出。有人可以帮忙吗?

public class TestCenter implements Comparable<TestCenter>{


    private Integer no_of_accessible_tests=0;
    private Integer tagId;
    private String tag;
    private Integer number_of_tests=0;

    private List<Test> tests= new ArrayList<>();

    public Integer getTagId() {
        return tagId;
    }
    public void setTagId(Integer tagId) {
        this.tagId = tagId;
    }
    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }
    public Integer getNumber_of_tests() {
        return number_of_tests;
    }
    public void setNumber_of_tests(Integer number_of_tests) {
        this.number_of_tests = number_of_tests;
    }
    public List<Test> getTests() {
        return tests;
    }
    public void setTests(List<Test> tests) {
        this.tests = tests;
    }

    public void addTest(Test t)
    {

        this.tests.add(t);
        this.number_of_tests++;
        if("Yes".equals(t.getAccess()))
            this.no_of_accessible_tests++;
    }
    @Override
    public int compareTo(TestCenter o) {

        if(this.no_of_accessible_tests.equals(o.no_of_accessible_tests))
        {
            return number_of_tests.compareTo(o.number_of_tests);
        }
        else
        {
            return o.no_of_accessible_tests.compareTo(this.no_of_accessible_tests);
        }
    }


}

System.out.println(gson.toJson(tests));
          System.out.println(mapper.writeValueAsString(tests));
          return mapper.writeValueAsString(tests);
output for both:
[{"no_of_accessible_tests":0,"tagId":196,"tag":"IBPS Clerical Prelims","number_of_tests":1,"tests":[{"activatedDate":null,"activatedTime":null,"ScheduleDate":null,"ScheduleTime":null,"endDate":null,"endTime":null,"testId":2193,"testCode":"P15 CP FLT 61","testName":"Amazon -4","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":["2017","IBPS Clerical Prelims"]}]},{"no_of_accessible_tests":0,"tagId":0,"tag":"Others","number_of_tests":6,"tests":[{"activatedDate":null,"activatedTime":null,"ScheduleDate":null,"ScheduleTime":null,"endDate":null,"endTime":null,"testId":2525,"testCode":"CP 16 FLT 04","testName":"Tech Mahindra -4","testType":"flt","testPattern":"ibps","testDurationInMinutes":65,"numberOfQuestions":76,"access":"No","status":"start","testResult":null,"tags":null},{"activatedDate":"2015-12-17","activatedTime":"09:00:00","ScheduleDate":"2015-12-17","ScheduleTime":"09:00:00","endDate":"license end date","endTime":"license end time","testId":2188,"testCode":"P15 CP FLT 58","testName":"Amazon -1","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"Resume","testResult":null,"tags":null},{"activatedDate":"2015-12-17","activatedTime":"09:00:00","ScheduleDate":"2015-12-17","ScheduleTime":"09:00:00","endDate":"license end date","endTime":"license end time","testId":2190,"testCode":"P15 CP FLT 59","testName":"Amazon -2","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":null},{"activatedDate":null,"activatedTime":null,"ScheduleDate":null,"ScheduleTime":null,"endDate":null,"endTime":null,"testId":2192,"testCode":"P15 CP FLT 60","testName":"Amazon -3","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":null},{"activatedDate":null,"activatedTime":null,"ScheduleDate":null,"ScheduleTime":null,"endDate":null,"endTime":null,"testId":1929,"testCode":"P15 CP FLT 09","testName":"CapGemini -1","testType":"flt","testPattern":"ibps","testDurationInMinutes":60,"numberOfQuestions":75,"access":"No","status":"start","testResult":null,"tags":null},{"test_ready":"no","reason_student_side":"This test would be available on 22-Dec-16.","reason_admin_side":".","testId":2633,"testCode":"BP FLT 87","testName":"SBI Specialist Officer Test -5","testType":"flt","testPattern":"ibps","testDurationInMinutes":90,"numberOfQuestions":120,"access":"No","status":"start","testResult":null,"tags":null}]}]
[{"tagId":196,"tag":"IBPS Clerical Prelims","number_of_tests":1,"tests":[{"testId":2193,"testCode":"P15 CP FLT 61","testName":"Amazon -4","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":["2017","IBPS Clerical Prelims"],"activatedDate":null,"activatedTime":null,"endDate":null,"endTime":null,"scheduleDate":null,"scheduleTime":null}]},{"tagId":0,"tag":"Others","number_of_tests":6,"tests":[{"testId":2525,"testCode":"CP 16 FLT 04","testName":"Tech Mahindra -4","testType":"flt","testPattern":"ibps","testDurationInMinutes":65,"numberOfQuestions":76,"access":"No","status":"start","testResult":null,"tags":null,"activatedDate":null,"activatedTime":null,"endDate":null,"endTime":null,"scheduleDate":null,"scheduleTime":null},{"testId":2188,"testCode":"P15 CP FLT 58","testName":"Amazon -1","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"Resume","testResult":null,"tags":null,"activatedDate":"2015-12-17","activatedTime":"09:00:00","endDate":"license end date","endTime":"license end time","scheduleDate":"2015-12-17","scheduleTime":"09:00:00"},{"testId":2190,"testCode":"P15 CP FLT 59","testName":"Amazon -2","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":null,"activatedDate":"2015-12-17","activatedTime":"09:00:00","endDate":"license end date","endTime":"license end time","scheduleDate":"2015-12-17","scheduleTime":"09:00:00"},{"testId":2192,"testCode":"P15 CP FLT 60","testName":"Amazon -3","testType":"flt","testPattern":"ibps","testDurationInMinutes":120,"numberOfQuestions":104,"access":"No","status":"start","testResult":null,"tags":null,"activatedDate":null,"activatedTime":null,"endDate":null,"endTime":null,"scheduleDate":null,"scheduleTime":null},{"testId":1929,"testCode":"P15 CP FLT 09","testName":"CapGemini -1","testType":"flt","testPattern":"ibps","testDurationInMinutes":60,"numberOfQuestions":75,"access":"No","status":"start","testResult":null,"tags":null,"activatedDate":null,"activatedTime":null,"endDate":null,"endTime":null,"scheduleDate":null,"scheduleTime":null},{"testId":2633,"testCode":"BP FLT 87","testName":"SBI Specialist Officer Test -5","testType":"flt","testPattern":"ibps","testDurationInMinutes":90,"numberOfQuestions":120,"access":"No","status":"start","testResult":null,"tags":null,"test_ready":"no","reason_student_side":"This test would be available on 22-Dec-16.","reason_admin_side":"."}]}]

它没有在输出中给出no_of_accessible_tests字段

1 个答案:

答案 0 :(得分:0)

感谢@Scary Wombat。 似乎问题是该领域没有getter / setter。