「Gradle」@JsonIgnoreでJavaオブジェクトの指定フィールドをjson文字列に変換しない

2021年10月21日

構文
ObjectMapper mapper = new ObjectMapper();
String json文字列= mapper.writeValueAsString(javaオブジェクト名);

使用例
1.build.gradle(gradleプロジェクトの設定ファイル)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
dependencies {
  // jackson-databindの導入
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0'
}
dependencies {   // jackson-databindの導入 implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0' }
dependencies {
     // jackson-databindの導入
     implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0'
}

2.クラスの定義
書式
@JsonIgnore
public String フィールド名;
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.course;
public class User {
public int age;
public String username;
// addrフィールドが変換の対象外
@JsonIgnore
public String addr;
}
package com.arkgame.course; public class User { public int age; public String username; // addrフィールドが変換の対象外 @JsonIgnore public String addr; }
package com.arkgame.course;

public class User {

      public int age;
      public String username;
      
      // addrフィールドが変換の対象外
      @JsonIgnore
      public String addr;

}

3.実行クラス(main)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.course;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class ObjJsonDemo {
public static void main(String[] args) throws JsonProcessingException {
// Userのインスタンスの生成
User user = new User();
// クラスのメンバーの値を設定
user.username = "山田 太郎";
user.age = 32;
user.addr = "東京品川区";
// ObjectMapper変数の宣言
ObjectMapper mapper = new ObjectMapper();
// JSON文字列をインデントする
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// JavaオブジェクトをJSON文字列に変換
String res = mapper.writeValueAsString(user);
System.out.println("JSON文字列をインデントして表示:\n" + res);
}
}
package com.arkgame.course; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class ObjJsonDemo { public static void main(String[] args) throws JsonProcessingException { // Userのインスタンスの生成 User user = new User(); // クラスのメンバーの値を設定 user.username = "山田 太郎"; user.age = 32; user.addr = "東京品川区"; // ObjectMapper変数の宣言 ObjectMapper mapper = new ObjectMapper(); // JSON文字列をインデントする mapper.enable(SerializationFeature.INDENT_OUTPUT); // JavaオブジェクトをJSON文字列に変換 String res = mapper.writeValueAsString(user); System.out.println("JSON文字列をインデントして表示:\n" + res); } }
package com.arkgame.course;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class ObjJsonDemo {

      public static void main(String[] args) throws JsonProcessingException {

            // Userのインスタンスの生成
            User user = new User();
            // クラスのメンバーの値を設定
            user.username = "山田 太郎";
            user.age = 32;
            user.addr = "東京品川区";

            // ObjectMapper変数の宣言
            ObjectMapper mapper = new ObjectMapper();
            // JSON文字列をインデントする
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            // JavaオブジェクトをJSON文字列に変換
            String res = mapper.writeValueAsString(user);

            System.out.println("JSON文字列をインデントして表示:\n" + res);

      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
JSON文字列をインデントして表示:
{
"age" : 32,
"username" : "山田 太郎"
}
JSON文字列をインデントして表示: { "age" : 32, "username" : "山田 太郎" }
JSON文字列をインデントして表示:
{
  "age" : 32,
  "username" : "山田 太郎"
}

 

gradle

Posted by arkgame