简体   繁体   English

Java的。根据WSDL验证SOAP消息

[英]Java. Validate SOAP message against a WSDL

I need to validate a SOAP message against a WSDL ? 我需要针对WSDL验证SOAP消息吗? (in the same way that an XML file can be validated against a XSD schema). (与可以针对XSD架构验证XML文件的方式相同)。

I am not calling any webservice; 我没有打电话给任何网络服务; I just have a SOAP message and a WSDL , and I need to verify that the SOAP message is correct. 我只有一个SOAP消息和一个WSDL ,我需要验证SOAP消息是否正确。 without calling the webservice or whatsoever afterwards. 无需调用网络服务或之后的任何内容。

I need to make this validate within a Java program. 我需要在Java程序中进行此验证。 Do you know of a small Java library to do this? 你知道一个小的Java库吗?

ps: I am aware that several JAX-WS libraries can validate the request/response when you call a webservice. ps:我知道几个JAX-WS库可以在您调用Web服务时验证请求/响应。 But again, I am not calling any webservice; 但同样,我没有打电话给任何网络服务; I have a simple SOAP message, and a WSDL , and I a need a function that validates the SOAP message against the WSDL . 我有一个简单的SOAP消息和一个WSDL ,我需要一个根据WSDL验证SOAP消息的函数。

ps: I am also aware that there tools that can do this, such as SOAPUI and XMLSpy . ps:我也知道有些工具可以做到这一点,比如SOAPUIXMLSpy Again, I need to do this validation within my Java program. 同样,我需要在我的Java程序中进行此验证。

ps: I am aware that I could extract the body part of the SOAP message, and validate it against the XSD . ps:我知道我可以提取SOAP消息的正文部分,并根据XSD对其进行验证。 However, I'd like to validate the entire SOAP message against the WSDL . 但是,我想根据WSDL验证整个SOAP消息。

You might look at the source code for the WsdlValidator class in the open-source soapUI project. 您可以在开源soapUI项目中查看WsdlValidator类的源代码。

https://github.com/SmartBear/soapui https://github.com/SmartBear/soapui

http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/support/wsdl/WsdlValidator.html http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/support/wsdl/WsdlValidator.html

If you are creating a webservice client using a code generator based on Apache CXF or Apache Axis 2 , Chances are high that your webservice code should work just fine as long as both the webservice provider and your client are in the same version of SOAP/WS* standards .Based on the API you choose to invoke the webservice you can see whether the logging capability of that API can be used to print out the SOAP request generated . 如果您使用基于Apache CXF或Apache Axis 2的代码生成器创建Web服务客户端,只要Web服务提供者和您的客户端都在相同版本的SOAP / WS中,您的Web服务代码就可以正常运行*标准。基于您选择调用Web服务的API,您可以查看该API的日志记录功能是否可用于打印生成的SOAP请求。

Another approach could be to use a tool like SOAPUI. 另一种方法可能是使用像SOAPUI这样的工具。 You could try to setup a mock webservice using SOAP UI based on the WSDL you have and then call test your webservice client by calling the mock service . 您可以尝试使用基于您拥有的WSDL的SOAP UI设置模拟Web服务,然后通过调用模拟服务来调用测试您的Web服务客户端。 See SOAP UI Link here http://www.soapui.org/Getting-Started/web-service-sample-project/1-Web-Service-Mocking.html 请参阅SOAP UI链接http://www.soapui.org/Getting-Started/web-service-sample-project/1-Web-Service-Mocking.html

Try this 试试这个

  1. Use the WSDL and create a Jax-RPC proxy 使用WSDL并创建Jax-RPC代理
  2. Create a webservice JAX RPC handler and register it to the webservice reference Please refer this link to see howto create webservice handlers 创建一个Web服务JAX RPC处理程序并将其注册到Web服务引用请参阅此链接以了解如何创建Web服务处理程序
  3. In the handler, try the below code in the public boolean handleRequest(MessageContext mc) add these lines 在处理程序中,尝试使用public boolean handleRequest(MessageContext mc)的以下代码添加这些行

     mc.setProperty("USE_TEST_FILE", <your response in .xml file>); return false; 
  4. In the public boolean handleResponse(MessageContext mc) public boolean handleResponse(MessageContext mc)

     String testFileName = (String) mc.getProperty("USE_TEST_FILE"); InputStream instream = <read file as an input stream> SOAPMessageContext smc = (SOAPMessageContext) mc; SOAPMessage message = smc.getMessage(); SOAPPart soapPart = message.getSOAPPart(); soapPart.setContent(new StreamSource(instream)); smc.setMessage(message); return false; 

now run your proxy.. the proxy will fail if the message is not valid per WSDL 现在运行你的代理..如果消息对每个WSDL无效,代理将失败

You can create a stub using the WSDL you have and then make the query to the stub using your XML request. 您可以使用您拥有的WSDL创建存根,然后使用XML请求对存根进行查询。 If this goes properly your request is correct. 如果这一点正确,您的请求是正确的。

You can import the WSDL in your project and then make a call to the stub service using your XML. 您可以在项目中导入WSDL,然后使用XML调用存根服务。 This way you can make changes to the validations without any change in the code. 这样,您可以在不更改代码的情况下更改验证。

As SOAP message is xml, you can validate whole message against xsd. 由于SOAP消息是xml,因此您可以针对xsd验证整个消息。 For example I found this schema for SOAP message. 例如,我发现了SOAP消息的这个模式。 You could: 你可以:

  1. Extract schema from your wsdl, 从wsdl中提取模式,
  2. Validate message against schema from wsdl and soap schema. 从wsdl和soap模式验证针对模式的消息。

Should you want to validate against specific WSDL, you can prepare you schema by hand: just place root element of your message instead of this part: 如果要对特定的WSDL进行验证,可以手动为模式做好准备:只需放置消息的根元素而不是此部分:

<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>

in SOAP schema. 在SOAP模式中。

Validate against the SOAP envelope schema , then extract body and headers and validate against specific schema. 验证SOAP信封架构 ,然后提取正文和标头,并针对特定架构进行验证。 Should be simple enough. 应该够简单。

I think that you may be looking for an answer which is way too complex for what you really need. 我认为你可能正在寻找一个对你真正需要的东西过于复杂的答案。 All you really need to do is have the appropriate DTD/XSD documents registered so that the normal JAXP classes can reference them. 您真正需要做的就是注册相应的DTD / XSD文档,以便普通的JAXP类可以引用它们。

This code, from Sun/Oracle , implements a basic EntityResolver . 此代码来自Sun / Oracle ,实现了一个基本的EntityResolver You need to build this class such that it will provide the DocumentBuilder with the appropriate InputSource s for the schema defined in the documents you are trying to validate. 您需要构建此类,以便它为DocumentBuilder提供适用于您尝试验证的文档中定义的模式的InputSource

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class MyResolver implements EntityResolver {
    public InputSource resolveEntity (String publicId, String systemId) {
        if (systemId.equals("http://www.myhost.com/today")) {
            // return a special input source
            MyReader reader = new MyReader();
            return new InputSource(reader);
        } else {
            // use the default behaviour
            return null;
        }
    }
}

Using this code, you can pull in your custom EntityResolver and have it used during the parsing/validation of the document. 使用此代码,您可以EntityResolver自定义EntityResolver并在解析/验证文档期间使用它。

final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware = true;
dbf.setValidating = true;
final DocumentBuilder db = dbf.newDocumentBuilder();
final MyResolver r = MyResolver.newInstance();
db.setEntityResolver(r);
final Document docFromFile = db.parse("path/to/my/document.xml");
// or
final Document docFromStream = db.parse(new FileInputStream("blah"));

请查看Apache AXIS2 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM