Mit Eclipselink Moxy können Sie Eigenschaften hinzufügen, um die Ausgabe zu manipulieren.
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();
}
}
}
Wird in: {"Buch": {"title": "hello"}} führen
Sowohl org.eclipse.Persistence.Moxy-2.5.1.jar und org.eclipse.persistence.core-2.5.1.jar sind auf dem Klassenpfad erforderlich. Während ich selbst damit herumspielte, stieß ich auf: Heißeste JAXB Antworten. Besonders blaise Doughan antwortet, wo sehr hilfreich ist. Suchen nach
MarshallerProperties.MEDIA_TYPE, "application/json"
für mehr seiner Beispiele.