4

I'm trying to use annotations at package level but I get compilation erros from Eclipse.

I have a class Head with the following package/annotation:

 @javax.xml.bind.annotation.XmlSchema (
    xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "com", 
                 namespaceURI="http://es.indra.transporte.common"),
      @javax.xml.bind.annotation.XmlNs( namespaceURI="http://www.w3.org/2001/XMLSchema")          
    },
    namespace = "http://es.indra.transporte.common", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
  )  
package es.indra.transporte.central.thalesinterface.common.beans;

I have created a package-info.java in es.indra.transporte.central.thalesinterface.common.beans folder with the above code but I'm still getting the compilation error

Package annotations must be in file package-info.java

in Head class. I'm using jdk6.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Fran
  • 157
  • 2
  • 2
  • 6

1 Answers1

4

The only problem I got when trying to compile your package info was that the @XmlNs annotation was missing the prefix property.

This:

@javax.xml.bind.annotation.XmlNs( namespaceURI="http://www.w3.org/2001/XMLSchema")

Should be:

@javax.xml.bind.annotation.XmlNs(prefix="xsd",  namespaceURI="http://www.w3.org/2001/XMLSchema")

The following corrected code should compile:

@javax.xml.bind.annotation.XmlSchema (
    xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "com", 
                 namespaceURI="http://es.indra.transporte.common"),
      @javax.xml.bind.annotation.XmlNs(prefix="xsd", namespaceURI="http://www.w3.org/2001/XMLSchema")
    },
    namespace = "http://es.indra.transporte.common", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
  )  
package es.indra.transporte.central.thalesinterface.common.beans;

For an example see:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Hi, I'm afraid the prefix is not my problem. I have set it but I still get the same error. – Fran Jan 31 '11 at 08:43
  • Checking the build folder I discovered that package-info.java file is not compiled, I don't know why but I can not generate the .class file and I guess this is the cause of the issue. – Fran Jan 31 '11 at 08:46
  • Hi again, I achieved to generate the package-info.class file but I still get the same error. I'm completely lost. – Fran Jan 31 '11 at 09:12
  • Fran - The only way I'm able to reproduce the error is to add that annotation to a file called something other than "package-info.java". Is there any chance you incorrectly have this annotation on another class (you mentioned the Head class)? In the Eclipse "Problems" tab what does it specify as the "Resource" with the problem? – bdoughan Jan 31 '11 at 14:14
  • Hi Blaise, I have partially solved the issue. It seems, as per I have found in this link (http://wiki.eclipse.org/Dali/Indigo/JAXB_2.x/PackageAnnotations#.40XmlSchema) in Eclipse it is only possible to annotate the package-info.java file. Annotating any other package declaration leads to a compile error. So I have deleted the annotation in "Head" class just leaving it in the package-info.java file and the compilation error has dissapeared. – Fran Jan 31 '11 at 15:11
  • But now I have other problem with the prefixes, I don't know why but when I generate the xml file I get "ns1" and "ns2" prefixes in the elements instead of "com" and "xsd" prefixes as I set in the annotation. But maybe this is a different issue and I should open a new post. – Fran Jan 31 '11 at 15:15
  • Hi Fran - The JAXB spec does not guarantee what prefixes will be used in a marshalled document. In Metro (the JAXB RI) you can use the NamespacePrefixMapper extension to control this. EclipseLink JAXB (MOXy) will use the prefixes as specified in the @XmlSchema annotation. There are some Stack Overflow questions on this already. Check out the following for a discussion on the various approaches: http://stackoverflow.com/questions/3289644/define-spring-jaxb-namespaces-without-using-namespaceprefixmapper – bdoughan Jan 31 '11 at 15:25
  • Hi Blaise, thanks for the help on the prefix issue, but now I have another problem. I can't get to show in the xml file all the parameters configured in the @xmlSchema annotation. For example, if I set: @javax.xml.bind.annotation.XmlSchema( namespace = "es.indra.transporte.common";, location = "es.indra.transporte.configuration StationNetwork.xsd", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package es.indra.transporte.central.thalesinterface.common.beans; – Fran Feb 01 '11 at 14:01
  • Which JAXB implementation are you using? May be worth starting a new question. – bdoughan Feb 01 '11 at 14:03
  • Sorry for the inconveniences, I have troubles to manage the new comments. I'm goint to open a new different question. This can be closed. Thanks – Fran Feb 01 '11 at 14:12