6

I followed this example: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JSON_Twitter

Now I have this class:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        StreamSource source = new StreamSource("http://test.url/path/to/resource");
        JAXBElement<Foo> jaxbElement = unmarshaller.unmarshal(source, Foo.class);

        System.out.println(jaxbElement.getValue().getFoo());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.marshal(jaxbElement, System.out);
    }
}

And I have jaxb.properties:

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

If I run this code, I get:

Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.setProperty(AbstractUnmarshallerImpl.java:352)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.setProperty(UnmarshallerImpl.java:450)
    at com.example.JavaSEClient.main(JavaSEClient.java:19)

How can I fix this?

I searched SO and Google, these answers are not working:

PropertyException when setting Marshaller property with eclipselink.media-type value: application/json JAXB javax.xml.bind.PropertyException

Community
  • 1
  • 1
Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74

1 Answers1

9

You need to make sure that your jaxb.properties file is in the same package as the domain classes you used to bootstrap the JAXBContext, and that EclipseLink MOXy is on your class path.

If you are using Maven, then the jaxb.properties file should be under the following location assuming Foo is in a package called com.example.Foo:

  • src/main/resources/com/example/foo/jaxb.properties
  • src/main/java/com/example/foo/Foo.class

For a full example see:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks. This solved my problem. I have another question, please help. I have a REST Jersey(2.7) server, which uses MOXy because I need entity filters. I want to make a REST client on android. I tried JBoss RESTEasy-mobile which was good for a while, but this uses Jackson, and MOXy and Jackson conflicts MAP. So now I want to use MOXy on android, but it looks I cant. Can I? How can I solve this? – Gergely Fehérvári Apr 03 '14 at 16:36
  • thank you for the hint with the package structure, much better then placing them in the source-folder! – Java_Waldi Jun 25 '14 at 11:22
  • 1
    You must place the jaxb.properties in the same file/package structure as the _model_ and not the marshaller code. In this example, `Foo` is the model, not the name of the marshaller demo. Running `System.out.println(jaxbContext);` really helps to debug (as was done in the blog demo). If you run that and do not see `class org.eclipse.persistence.jaxb.JAXBContext`, you are not loading the correct `JAXBContext`. – Michael Plautz May 27 '15 at 04:10
  • i have a springboot project, so i have controller, beans, application portal, where should I put this jaxb.resources then? Thanks! – wwwwan Jul 01 '19 at 17:53