XmlElement annotation dissallowed with WebParam
Asked Answered
F

2

16

I have a method inside a webservice, with the following signature:

@WebResult(name="purchaseId") public int CreatePurchase(
            @XmlElement(required=true)
            @WebParam(name = "item") String item {
  ...
}

It seems to me (based on what information i've found) that this should work. Unfortunately, I get the following error message on compilation:

The annotation @XmlElement is disallowed for this location

Does anyone know how to resolve the issue?

Funchal answered 21/11, 2011 at 11:56 Comment(4)
Are you using any specific JAXB implementation, or just what's provided by Java EE or the web container? The reference implementation really dislikes non-JAXB annotations getting in its way.Blacksnake
Not 100% sure I understand the question (I'm rather new to Java EE), but Apache CFX 2.3.7 is included in the project build path, and that contains some jaxb JARs.Funchal
This two may helpSteverson
Thank you, I've read through them. Unfortunatley, I couldn't find anything in those threads that helped me solve the problem.Funchal
S
34

JAX-B is included with a JDK by default. The version that comes with the particular JDK isn't updated nearly as frequently as JAX-B itself. The current version that comes with the JDK (1.6) is JAX-B 2.1.10 (documented here).

@XmlElement is only allowed on method parameters starting with JAX-B 2.2

When Java loads libaries it loads libraries that come with the JDK before it loads libraries that are on the classpath. Upgrdading to Java 7 would fix your problem. There is also a process for telling Java that you want to use a more up-to-date library if you aren't able to upgrade to Java 7. These are called "endorsed" libraries and you have to put the library in the same folder structure as the JDK itself. The process is described here.

Sprag answered 21/11, 2011 at 14:27 Comment(2)
wow, thnx a lot, fixed the problem along with a nice piece of info.Kwon
I am Using Java 7 as default JRE but same error is happing to me also. Can you please help me to resolve this Issue.Dumpy
B
2

When you are having the following error message: "The annotation @XmlElement is disallowed for this location", chances are that you're using the wrong import statement.

Change it to:

import javax.xml.bind.annotation.XmlElement;

As Eclipse suggests another package as the first option, it's a very common mistake.

Blowhole answered 11/12, 2016 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.