Exception in thread “main” javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json

前端 未结 3 915
鱼传尺愫
鱼传尺愫 2021-02-12 16:28

I\'m attempting to follow the example located here but get an javax.xml.bind.PropertyException. I receive this exception because of the following line of code:

m         


        
3条回答
  •  忘掉有多难
    2021-02-12 17:10

    If you don't want to add a jaxb.properties file, you can do this all in Java code. This is especially helpful for legacy systems where you don't want to risk affecting the classpath by introducing a new jaxb.properties file.

    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory;
    
    //Set the various properties you want
    Map properties = new HashMap<>();
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
    properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    
    //Create a Context using the properties
    JAXBContext jaxbContext = 
        JAXBContextFactory.createContext(new Class[]  {
           MyClass.class,    ObjectFactory.class}, properties);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    
    //Marshall the object
    StringWriter stringWriter = new StringWriter();
    jaxbMarshaller.marshal(myObject, stringWriter);
    String json = stringWriter.toString();
    

提交回复
热议问题