java 在 JAX-WS 代码优先方法中根据模式验证soap 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27398060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:44:08  来源:igfitidea点击:

Validate soap requests against schema in a JAX-WS code-first approach

javasoapwsdlcxfjax-ws

提问by kgautron

I created a JAX-WS webservice, using JAXB annotations on some request fields to make them mandatory.

我创建了一个 JAX-WS web 服务,在一些请求字段上使用 JAXB 注释来使它们成为强制性的。

@XmlElement(required = true)
protected String number;

The WSDL generated by cxf-java2ws-plugin is correct, there is no minOccurs="0"on the fields :

cxf-java2ws-plugin 生成的 WSDL 是正确的,minOccurs="0"字段上没有:

<xs:element name="number" type="xs:string"/>

But when the service receives a request that does not respect these constraints (missing fields), no SoapFault or exception is thrown.

但是当服务收到不遵守这些约束(缺少字段)的请求时,不会抛出 SoapFault 或异常。

I also tried adding @SchemaValidationto my WS class, with no effect.

我也尝试添加@SchemaValidation到我的 WS 类中,但没有效果。

How request validation against schmema (or rather validation against annotation-based constraints) can be automated?

对 schmema 的请求验证(或者更确切地说是对基于注释的约束的验证)如何可以自动化?

回答by Gren

The default value for minOccurs is 1. So, your value must exist.
The default value for nillable is false. So, your value cannot be empty.
And you have activated the schema validation in your web service:

minOccurs 的默认值为 1。因此,您的值必须存在。
nillable 的默认值为 false。因此,您的值不能为空。
并且您已经在您的 Web 服务中激活了架构验证:

@WebService
@SchemaValidation(enabled = true)
public class MyService {
//...
}

And, last but not least, you have a resulting schema.

而且,最后但并非最不重要的是,您有一个结果模式。

But, the JAXB reference implementation does not any validation for you.

但是,JAXB 参考实现不会对您进行任何验证。

You have to do this by your own, using the Java Xml Validation API

您必须使用Java Xml Validation API自行完成此操作

Here a simple example of schema validation

这是模式验证的简单示例

public class XmlSchemaValidation {

    public static void main(String[] args) throws Exception {
        Something myObject = new Something();
        validate(myObject);
    }
    public static void validate(Something myObject) throws Exception {

        Schema s = SchemaFactory
                   .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                   .newSchema(new File("Something.xsd"));

        Validator validator = s.newValidator();
        //You can set an error handler
        //validator.setErrorHandler(errorHandler);
        validator.validate( new JAXBSource(JAXBContext.newInstance(Something.class), myObject));
    }

}

回答by KevinHol

I propose two workarounds to your problem, if you can't solve it :

如果您无法解决问题,我提出了两种解决方法:

  1. Validate your bean after JAXB unmarshaling
  2. Use CXF Validation Feature
  1. 在 JAXB 解组后验证您的 bean
  2. 使用 CXF 验证功能

Validation after JAXB unmarshaling

JAXB 解组后的验证

I think you can add a JAXB callback afterUnmarshal() in your Java request bean (as described here), and perform all validation you want in it (format validation, or others).

我认为您可以在 Java 请求 bean 中添加 JAXB 回调 afterUnmarshal()(如此处所述),并在其中执行您想要的所有验证(格式验证或其他)。

For example :

例如 :

@XmlRootElement
public class YourRequest {

    @XmlElement(required = true)
    protected String number;

    // some code here

    public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
       if(number == null) {
           // Throw validation exception
       }
    }

}

CXF Validation feature

CXF 验证功能

Another possibility is to use Bean Validation with CXF (as documented here). Usually, it's not necessary for a schema-based validation, but if you need to have a more complex validation than a schema-based one, I think it can be a solution to your problem after all.

另一种可能性是在 CXF 中使用 Bean 验证(如此处所述)。通常,基于模式的验证不是必需的,但是如果您需要比基于模式的验证更复杂的验证,我认为它毕竟可以解决您的问题。

I hope it can help you while waiting a better answer.

我希望它可以帮助您等待更好的答案。

回答by sitakant

Use some tool like jaxb2-maven-plugin to generate the JAXB Class. and Validate before sending the payload to the WebService or before accepting the payload in to your system using JSR 303 Specification. Click Hereto see the JSR 303 Validator.

使用像 jaxb2-maven-plugin 这样的工具来生成 JAXB 类。在将有效负载发送到 WebService 之前或在使用 JSR 303 规范将有效负载接收到您的系统之前进行验证。 单击此处查看 JSR 303 验证器。

This will make your system Robust.

这将使您的系统健壮。