16

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?

unj2
  • 52,135
  • 87
  • 247
  • 375

5 Answers5

12

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • If I just move javax.xml.bind.annotation.* I get errors when compiling. One example - `DOMHandler` - `No source code is available for type javax.xml.transform.Result`. Am I missing something? – Andy Smith Nov 27 '12 at 21:15
  • You need complete source code for all classes/interfaces/annotations that are referenced from the sources. Also copy over the source for `javax.xml.transform.Result`. – Tassos Bassoukos Nov 29 '12 at 12:03
  • Thanks - I'd copied over everything from javax.xml.bind but needed to just limit it to my dependencies. Are you using this to parse xml or something different? I am trying to parse XML and am how have dependency troubles if I either unmarshal server side (difficulties serializing the jaxb class) or client side (further dependency issues). – Andy Smith Dec 05 '12 at 08:00
  • JAXB is an astoundingly bad match for the GWT environment; I would seriously suggest finding some other way to do your (de-)serialization. My use-case was the reuse of my own domain classes in the GWT client, and I needed a way to make the compiler ignore the JAXB-specific annotations. The domain objects were transferred to the client via the normal GWT RPC/serialization mechanism – Tassos Bassoukos Dec 05 '12 at 09:57
  • Thanks Tassos - I was starting to consider ditching JAXB but this post (and one or two others) gave me a glimmer of hope, but I see that you've got a totally different use case. Thanks for your help, I will try something else :) – Andy Smith Dec 05 '12 at 10:34
1

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.

pfranza
  • 3,292
  • 2
  • 21
  • 34
0

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>
0

You might find a viable solution here: GWT with JPA

Community
  • 1
  • 1
Stijn Van Bael
  • 4,830
  • 2
  • 32
  • 38