Eclipselink Moxyを使用して、プロパティを追加して出力を操作できます。
public class Main {
public static void main(String[] args) {
Book book = new Book();
book.title = "hello";
JAXBContext context;
try {
context = JAXBContextFactory.createContext(new Class[] {Book.class}, null);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(book, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
結果:{"book":{"title": "hello"}}
org.eclipse.persistence.moxy-2.5.1.jarとorg.eclipse.persistence.core-2.5.1.jarの両方がクラスパスで必要です。自分でこれを遊んでいる間、私は次のように遭遇しました 最もホットなJaxbの回答. 。特にブレイズドーガンは非常に役立つ場所で答えます。検索する
MarshallerProperties.MEDIA_TYPE, "application/json"
彼の例の詳細については。