かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

JAXBでJSON出力したい

  • eclipselinkの2.4.0を入れる
  • jsonに出力したいクラスのあるパッケージにjaxb.propertiesを置く
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
  • JAXBContextを生成するときにJAXBContextProperties.MEDIA_TYPEにapplication/jsonを設定する
    • JAXBContextPropertiesはeclipselinkにあるクラスなので注意
Map<String, String> properties = new HashMap<>();
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
JAXBContext ctx = JAXBContext.newInstance(new Class<?>[] { clazz }, properties);


これでmarshal, unmarshalするとjsonになる!