باستخدام 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 ضرورية على classpath. أثناء اللعب مع هذا بنفسي ، ركضت إلى: سخونة إجابات Jaxb. لا سيما بليز دوغان يجيب حيث مفيد جدا. البحث عن
MarshallerProperties.MEDIA_TYPE, "application/json"
لمزيد من أمثلةه.