javasoapcxfwebservices-client

Nillable = false is not working in apache cxf


I am generating wsdl from java. I have given nillable = false in java field but the field accepts empty value from web service request. My bean is

  import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.format.annotation.DateTimeFormat;

@XmlRootElement(name = "LocationData")
@XmlAccessorType(XmlAccessType.FIELD)
public class LocationData {

    private String id;
    @DateTimeFormat(pattern="yyyy-mm-dd")
    private Date date;
    @NotNull
    @XmlElement(required=true,nillable=false)
    private String timezone;
    @XmlElement(required=true,nillable=false)
    private String location;

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLocation() {
        return location;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb, Locale.US);
        formatter.format("ID:%s\nLocation:%s\nDate:%s\nTime zone:%s\n", getId(), getLocation(), getDate(), getTimezone());

        return sb.toString();
    }
}

My Interface is

@WebMethod
    public LocationData createLocation( LocationData locationData) throws DuplicateLocationException;

Kindly let me know, what could be the issue? Am I missing anything? Any help would be appreciated.


Solution

  • May be another way is to validate is using SimpleType with min value and use schema validation.

    1. Enable Schema Validation.

      @WebMethod
      @SchemaValidation(type=SchemaValidationType.BOTH, schemas="mywsdl.wsdl")
      public LocationData createLocation( LocationData locationData) throws DuplicateLocationException;
      
    2. Modify your wsdl file to have restrictions on timezone

      <xs:element name="timezone">
        <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:minLength value="1" />
           </xs:restriction>
        </xs:simpleType>
      </xs:element>