dermoritz
dermoritz

Reputation: 13028

Strange "missing an @XmlRootElement annotation" error, only with Maven, works in Eclipse

I wrote code that marshals xml data. Before marshaling the data to output i validate the data the way Blaise Doughan suggests here

Running a simple test in eclipse (marshaling data an validating it) works fine, but as soon i run the test in console (mvn test) i get "missing an @XmlRootElement annotation".

The code is generated by moxy - without this annotation. And my Question is why it works in Eclipse? Or how to get it working also in console? The only explanation i have is that some dependencies are different (probably eclipse uses some internal xml stuff?)

I am aware of the possibility to wrap my root-element in JAXBElement - but for some reason it works in eclipse without annotation and without wrapping.

Upvotes: 1

Views: 1983

Answers (2)

bdoughan
bdoughan

Reputation: 149037

EclipseLink JAXB (MOXy) will marshal without an @XmlRootElement annotation, the the JAXB reference implementation will complain.

I suspect that you do not have the jaxb.properties file in the correct location of your Maven setup. If your domain model is in the com.example.foo package then the jaxb.properties file should go in the following location.

src/main/resources/com/example/foo/jaxb.properties 

For an example project see:

UPDATE

is it possible to tell moxy/the marshaller to use a fixed jaxb.properties and not to search it deep in package folders (i have many of the and all are identical)

If you want MOXy to always be used as the JAXB (JSR-222) provider then you can add the following to your classpath:

META-INF/services/javax.xml.bind.JAXBContext

The javax.xml.bind.JAXBContext file will contain the following contents:

org.eclipse.persistence.jaxb.JAXBContextFactory

Otherwise you can always use the native MOXy APIs to create the JAXBContext:

    JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {Foo.class}, null);

Upvotes: 1

dermoritz
dermoritz

Reputation: 13028

Blaise is right, the jaxb.properties must be under resource in same folder structure(package) as in src/main/java folders. In my case i have 20 of those files (and jaxb-classes for 20 xsd). To get it working without manually copying add to pom:

<build>
...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/jaxb.properties</include>
            </includes>
        </resource>
    </resources>
...
</build>

the first "resource" adds the normal resource folder. the 2nd adds src/main/java but includes only all jaxb.properties wherever they are.

Upvotes: 1

Related Questions