Using EclipseLink MOXy, you can add properties to manipulate the 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();
}
}
}
Will result in: { "book" : { "title" : "hello" } }
Both org.eclipse.persistence.moxy-2.5.1.jar and org.eclipse.persistence.core-2.5.1.jar are needed on the classpath. While playing around with this myself, I ran into: hottest JAXB answers. Especially Blaise Doughan answers where very helpful. Search for
MarshallerProperties.MEDIA_TYPE, "application/json"
for more of his examples.