Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@XmlElement(required=true) for @WebParam does not work

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? :(

like image 954
Shichao Avatar asked Oct 23 '11 21:10

Shichao


3 Answers

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?

like image 119
john.hestad Avatar answered Nov 15 '22 07:11

john.hestad


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

like image 4
Alex Bezverkhniy Avatar answered Nov 15 '22 08:11

Alex Bezverkhniy


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.

like image 2
Thiago Pasa Avatar answered Nov 15 '22 09:11

Thiago Pasa