5

I work on a project, made with Spring Framework, where i want to use JaxB, to convert an object to json, and i recieve this error:

javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(Unknown Source)
at com.fabbydesign.controller.DashboardController.main(DashboardController.java:82)

The code that i test is:

public static void main(String[] args) throws ParseException{

    ReturnBean rb = new ReturnBean();
    rb.setStatus(1);
    rb.setMessage("Message here!");

    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(ReturnBean.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.marshal(rb, System.out);

    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I have added in pom.xml EclipseLink dependency:

<dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.6.4</version>
    </dependency>

and, i have added the file jaxb.properties with the content:

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

I don't know why i still recieve this error, because, as i read on this forum i think i did all. What i want to mention, is, that the code that i test is in the directory:

src\main\java\com\fabbydesign\controller

and the file jaxb.properties is in the directory:

src\main\resources\com\fabbydesign\controller

What I did wrong?

FoldFence
  • 2,674
  • 4
  • 33
  • 57
fabby
  • 131
  • 3
  • 15

3 Answers3

5

Simple, you can specify system variable like:

System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory");

It worked for me.

Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
1

I'm not sure if the location of the jaxb.properties file is correct. The stacktrace still contains com.sun.xml.internal.bind.v2.runtime classes, which indicates that the wrong factory class is still being used.

have a look here for more info or alternative way to setup the factory: Where to include jaxb.properties file?

Community
  • 1
  • 1
Markus
  • 1,887
  • 18
  • 23
1

i found the problem: i had to add jaxb.properties in the package where ReturnBean class is located !!!

fabby
  • 131
  • 3
  • 15
  • that's what I meant with my answer above. The location of the jaxb.properties was wrong. good, you got it sorted out! – Markus Mar 03 '17 at 11:42