How to use JAXB annotations in the client side for

2019-02-03 05:27发布

问题:

I wanted to use the JaxB annotation in my class like this:

@XmlRootElement
public class ItemExtension implements Serializable {

But GWT complains when I put it in the client side.

 [ERROR] Line 4: The import javax.xml.bind cannot be resolved
         [ERROR] Line 14: XmlRootElement cannot be resolved to a type

Is there a workaround or am I doing something wrong?

回答1:

I have a project that uses entity classes with both JPA and JAXB annotations in the client-side GWT code. See the section "Overriding one package implementation with another" in the GWT Documentation.

Let's say your module is in package com.example.app. You will need to recreate[1] all JAXB annotation classes in a new package, specifically com.example.app.jre.java.xml.bind.annotation; in your module XML file, you then add <super-source path="jre" /> and you're set.

Note that you don't need to distribute the class files in that package, they are needed solely for the sake of the GWT compiler.

[1]: You can copy them over and adjust all package references.



回答2:

Have you checked these discussions already:

http://groups.google.com/group/google-web-toolkit/browse_thread/thread/849c6cc6c9b1467a/cab6b973bbf7d162?hl=en&lnk=gst&q=jaxb+annotations#cab6b973bbf7d162

and

http://groups.google.com/group/google-web-toolkit/browse_thread/thread/6312cedd281b91a9/b1add5932c842ace?hl=en&lnk=gst&q=jaxb+annotations#b1add5932c842ace



回答3:

I have used this library to do jaxb parsing from within a GWT client

http://code.google.com/p/gwtjaxb/

It is not a full JAXB generator, but it covers everything that I need.



回答4:

You might find a viable solution here: GWT with JPA



回答5:

I was able to solve my GWT compiler issues by excluding the generated JAXB ObjectFactory from the source in the GWT module XML:

<source path="model">
    <exclude name="**/ObjectFactory.*" />
</source>