Nillable = false 在 apache cxf 中不起作用

Nillable = false is not working in apache cxf

我正在从 java 生成 wsdl。我在 java 字段中给出了 nillable = false,但该字段接受来自 Web 服务请求的空值。我的豆子是

  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();
    }
}

我的界面是

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

请告诉我,可能是什么问题?我错过了什么吗?任何帮助将不胜感激。

可能是另一种验证方法是使用 SimpleType 和最小值并使用模式验证。

  1. 启用架构验证。

    @WebMethod
    @SchemaValidation(type=SchemaValidationType.BOTH, schemas="mywsdl.wsdl")
    public LocationData createLocation( LocationData locationData) throws DuplicateLocationException;
    
  2. 修改您的 wsdl 文件以对 timezone

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