如何在 JAX-WS/JAXB 中使用 schemaValidation 来验证非空元素
我正在使用 JAX-WS 实现 java-first Web 服务,并尝试使用 @SchemaValidation 来验证 webMethod 参数中所需的元素。 这是我声明我的必需属性的方式;
@XmlElement(required=true)
完美映射我的 wsdl 架构(minOccur 不再为 0),但 schemaValidation 不验证传递的 null 元素。
@XmlElement(required=true, nillable=false)
public String getClubName() {
return clubName;
}
任何使用我的 API 的客户端都可以传递 null 元素。我没有办法使用 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 来验证字符串值? 时不能这样做
<xsd:simpleType name="NotEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
一种方法是施加限制,但在使用 java-first web services相关帖子
; 验证生成的 JAXB 类 (JSR 303 / Spring)
<一个href="https://stackoverflow.com/questions/6006103/how-to-enable-schema-validation-so-that-jaxb-rejects-empty-element">如何启用模式验证以便 JAXB 拒绝空元素?
I am implementing java-first web services using JAX-WS and trying to use the @SchemaValidation
to validate required elements that are in my webMethod parameters.
here is how I declare my required attribute;
@XmlElement(required=true)
maps my wsdl schema perfectly(minOccur is not 0 anymore) but the schemaValidation in not validating the passed null elements.
@XmlElement(required=true, nillable=false)
public String getClubName() {
return clubName;
}
Any client that uses my API can pass null elements. I don't have a way to prevent this using JAXB.
On the other hand bean validation is working as follows. However not for String fields
@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
How can I use schemaValidation to validate string values? One way is to put restrictions but you can not do that when using java-first web services
<xsd:simpleType name="NotEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
related posts;
Validation for generated JAXB Classes (JSR 303 / Spring)
How to enable schema validation so that JAXB rejects empty element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,您可以使用 JAXB 来完成此操作,就像此处所述。或者可以使用此处所述的模式验证框架。但是,恕我直言,这两种方法都会给代码添加太多混乱,所以我会选择 Guava 的先决条件,它为您提供了一种快速而干净的方法来执行此操作。太糟糕了,没有对此的自动支持=/。
Apparently you can do it using JAXB like said here. Or maybe use the Schema Validation Framework as described here. But, IMHO, both the approaches add too much clutter to the code, so I'd go for Guava's Preconditions, which gives you a fast and clean way of doing this. Too bad there's no automatic support for this =/.