【问题标题】:how to validate not null elements using with schemaValidation in JAX-WS/JAXB如何在 JAX-WS/JAXB 中使用 schemaValidation 验证非空元素
【发布时间】:2011-10-19 07:04:47
【问题描述】:

我正在使用 JAX-WS 实现 Java 优先 Web 服务,并尝试使用 @SchemaValidation 来验证我的 webMethod 参数中的必需元素。 这是我声明所需属性的方式;

@XmlElement(required=true) 完美地映射了我的 wsdl 模式(minOccur 不再为 0),但 schemaValidation 没有验证传递的 null 元素。

@XmlElement(required=true, nillable=false)
public String getClubName() {
    return clubName;
}

任何使用我的 API 的客户端都可以传递空元素。我没有办法使用 JAXB 来防止这种情况发生。

另一方面,bean 验证的工作方式如下。但不适用于字符串字段

@Column(name = "FOUNDATION_YEAR", nullable = false) 
private Integer foundationYear;   //schemaValidation validates

@Column(name = "CLUB_NAME", length=100, nullable = false)
private String clubName;    //schemaValidation does not validate

如何使用 schemaValidation 来验证字符串值?一种方法是设置限制,但在使用 java-first web 服务时不能这样做

<xsd:simpleType name="NotEmptyString">
  <xsd:restriction base="xsd:string">
    <xsd:minLength  value="1"/>
  </xsd:restriction>
</xsd:simpleType>

相关帖子;

Validation for generated JAXB Classes (JSR 303 / Spring)

How to enable schema validation so that JAXB rejects empty element?

【问题讨论】:

    标签: jaxb jax-ws


    【解决方案1】:

    显然你可以像here 所说的那样使用 JAXB 来做到这一点。或者可以使用here 中描述的模式验证框架。但是,恕我直言,这两种方法都给代码增加了太多的混乱,所以我会选择Guava's Preconditions,它为您提供了一种快速而干净的方法。太糟糕了,没有自动支持这个=/。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2012-09-10
      • 2016-01-26
      • 1970-01-01
      • 2019-03-15
      相关资源
      最近更新 更多