Jackson-annotations-jacksoninject

提供:Dev Guides
移動先:案内検索

ジャクソン注釈-@JacksonInject

*@ JacksonInject* は、Json入力から解析される代わりにプロパティ値が挿入される場合に使用されます。 以下の例では、Jsonから解析するのではなく、オブジェクトに値を挿入しています。

例@JacksonInject

import java.io.IOException;
import java.text.ParseException;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTester {
   public static void main(String args[]) throws ParseException{
      String json = "{\"name\":\"Mark\"}";
      InjectableValues injectableValues = new InjectableValues.Std()
         .addValue(int.class, 1);

      ObjectMapper mapper = new ObjectMapper();
      try {
         Student student = mapper
            .reader(injectableValues)
            .forType(Student.class)
            .readValue(json);
         System.out.println(student.rollNo +", " + student.name);
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
}
class Student {
   public String name;
   @JacksonInject
   public int rollNo;
}

出力

1, Mark