「Java」ObjectMapperのsetSerializationInclusion()を利用するサンプル

サンプルコード

   public static class Sample {
        public int age = 20;
		public String user=null;
    }
    public static void test() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL);

        String result = mapper.writeValueAsString(new Sample());

        System.out.println("json data:"result);
    }

結果

json data:{“age":20}

Software

Posted by arkgame