Usando Eclipselink Moxy, è possibile aggiungere proprietà per manipolare l'output.
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();
}
}
}
Si tradurrà in: {"libro": {"Titolo": "Hello"}}
Entrambi org.eclipse.persistence.moxy-2.5.1.jar e org.eclipse.persistence.core-2.5.1.jar sono necessari sul percorso di classe. Mentre giocavo con questo io stesso, mi sono imbattuto: risponde jaxb più sexy. Soprattutto Blaise Doughan risponde dove molto utile. Cercare
MarshallerProperties.MEDIA_TYPE, "application/json"
Per altri suoi esempi.