15

I'm building web service using JAX-WS. I have a strange problem that the annotation @XmlElement(required=true) for @WebParam works in some @WebService class, but doesn't work in some others.

I have very similar code in the two @WebService classes. What may cause this problem? Parameter type or the entity class?

Edit: Add sample code

I have two web services:

@WebService(name = "ClubMemberPortType", serviceName = "ClubMemberService", portName = "ClubMemberSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubMemberWS {
@WebMethod(operationName = "findClubMembersByClubId", action = "urn:findClubMembersByClubId")
    @WebResult(name = "club_membership")
    public List<ClubMembership> findClubMembershipsByClubId(@XmlElement(required=true)
                                                        @WebParam(name = "club_id") String clubId, 
                                                        @WebParam(name = "status") StatusEnum status){
...
}}

and

@WebService(name = "ClubPortType", serviceName = "ClubService", portName = "ClubSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubWS {
@WebMethod(operationName = "findClubByClubId", action = "urn:findClubByClubId")
    @WebResult(name = "club")
    public Club findClubByClubId(@XmlElement(required=true)
                                @WebParam(name = "club_id") String clubId) {
...
}}

The generated schema for the first web method is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:findClubMembersByClubId>
         <club_id>?</club_id>
         <!--Optional:-->
         <status>?</status>
      </ws:findClubMembersByClubId>
   </soapenv:Body>
</soapenv:Envelope>

The generated schema for the second web method is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:findClubByClubId>
         <!--Optional:-->
         <club_id>?</club_id>
      </ws:findClubByClubId>
   </soapenv:Body>
</soapenv:Envelope>

So the first one works fine, the second one does not work. How is it possible? :(

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Shichao
  • 219
  • 1
  • 3
  • 14
  • Any code samples for the two cases? Actual problem you are facing (e.g. exceptions, better description of the behavior, etc)? – Gonzalo Garcia Lasurtegui Oct 23 '11 at 22:39
  • Please add the stacktrace of the error – Tomer Jun 21 '12 at 10:59
  • When I add @XmlElement get ERROR `The annotation @XmlElement is disallowed for this location` e.g `public boolean validateRegistration(@XmlElement(required = true) @WebParam String devicedId`. Java 1.6.45 – Ravi Parekh Jun 08 '15 at 14:59
  • Correct answer at https://stackoverflow.com/questions/8211420/xmlelement-annotation-dissallowed-with-webparam – Pino May 25 '18 at 13:35

5 Answers5

5

Adding @XmlElement(required=true,nillable=false) after @WebParam solved my similar problem. Using CXF 2.7.9. Did not try putting @XmlElement first, could it be that simple?

WilQu
  • 7,131
  • 6
  • 30
  • 38
john.hestad
  • 81
  • 1
  • 3
  • Getting The annotation `@XmlElement is disallowed for this location` . How to solve this issue . Using RSA for development. If i need to update cxf version , Kindly help with maven link with version – Rajesh Mar 30 '17 at 16:38
4

I have same problem. I found solution in using of separate class for parameters of service method.

e.g.

@XmlType(name="SampleRequestType", propOrder={"title", "ref"})
public class SampleRequest {
    @XmlElement(name="title", required=false)
    private String title;
    @XmlElement(name="ref", required=true)
    private String ref;
    ...

web-method

@WebMethod
public String sampleMethod(@WebParam(name = "params") SampleRequest params) {

maybe this will help

2

If you are having the following error message: "The annotation @XmlElement is disallowed for this location", chances are 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.

0

You try change the order in @WebParam and @XmlElement? actually i have a WS whit this:

public Persona consultarPersona(@WebParam(name = "cedula") @XmlElement(required=true, nillable=false, name="cedula", namespace="cedulaParam")  String cedula) 

And the description generate is:

 <xsd:schema>
<xsd:import namespace="cedulaParam" schemaLocation="http://eniacstein-pc:8080/WSDL_Sample/GestorPersonas?xsd=2"/>
</xsd:schema>

and the schema definition:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="cedulaParam">
<xs:element name="cedula" type="xs:string"/>
</xs:schema>

that's it!!

jthan24
  • 11
  • 1
0

Perhaps I found the solution. If anyone encounters the same problem, just place these annotations in you web service contract(interface) in exact this order, as all guys answered before.

@WebParam(name = "yourParam") @XmlElement(required = true)  YourParam param
Woods
  • 119
  • 1
  • 12
  • The order appears to be irrelevant (tested against CFX 3.4.2). @XmlElement(required = true) does indeed the job, probably depending on CXF's databinding (JAXB 2.x by default). Talking about job done: You need to turn on schema verification (default off) to let CFX check for the correct number of arguments. Otherwise all you get is a proper WSDL type definition. – wh81752 Jan 29 '21 at 12:55