19

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:

marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");

I have literally copy/pasted the example listed above so my code is exactly what you see there. Searching SO and Google for this has not been helpful, and thought I'd bring this to the geniuses at SO for some help. Any help would be most appreciated, (de)serialization with JSON and XML with json.org, Jackson, and JAXB has turned into a black and bottomless pit that has consumed almost a month of my life.

My first impression was that I wasn't properly specifying the eclipselink runtime (as described here) but that didn't produce a solution.

Stacktrace:

Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
   at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
   at HelloWorld.main(HelloWorld.java:17)

This is what I'm doing,

enter image description here

Community
  • 1
  • 1
user3167333
  • 247
  • 3
  • 4
  • 8

4 Answers4

22

You need to have the EclipseLink jars (2.4.0 or newer) on your class path, and a jaxb.properties file in the same package as the classes used to bootstrap theJAXBContext with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Below is a link to an example on GitHub that you can run to see everything working:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for the answer! Did u see my PrintScr? I have downloaded the last EclipseLink and have created the file named jaxb.properties. What could it be wrong? – user3167333 Jan 07 '14 at 04:00
  • Please elaborate the 'problem with jaxb'. – atripes Apr 08 '15 at 09:59
  • 8
    To make it painfully obvious: the `jaxb.properties` file must be in the same package in the *resources tree*, not in the *src tree*. See Blaise's example code. – Hank Aug 26 '15 at 08:44
  • 2
    Clarification `jaxb.properties` has to be in the package of the **model** classes. So in your example in the class where `Customer` class is. I tried to put the properties files to the package where my class corresponding to `HelloWorld` was. – skrii Oct 07 '15 at 13:58
14

To my main method I added (you could also use -D):

System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
zero298
  • 25,467
  • 10
  • 75
  • 100
weller1
  • 141
  • 1
  • 2
  • 4
    Welcome to SO. Please format the code in your answer and consider explaining why your answer works. Also, please look through the [how to answer guide](http://stackoverflow.com/help/how-to-answer). – Richard Erickson Feb 09 '16 at 18:34
8

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<String, Object> 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();
Jon
  • 3,212
  • 32
  • 35
0

It depends on the versions and works with maven for me with:

    openjdk version "17.0.4" 2022-07-19
    OpenJDK Runtime Environment (build 17.0.4+8-Ubuntu-120.04)
    OpenJDK 64-Bit Server VM (build 17.0.4+8-Ubuntu-120.04, mixed mode, sharing)

mvn:

        .
        .
        .
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.json</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.json.bind</groupId>
            <artifactId>jakarta.json.bind-api</artifactId>
            <version>2.0.0</version>
        </dependency>
        .
        .
        .

I post this because I got the mentioned exception above with the newest versions and found no solution. Maybe this setup helps.

Curlywurly
  • 11
  • 3