草庐IT

java - soap ui 生成的代码

coder 2024-03-21 原文

我有一个网络服务,我正在尝试为其构建客户端。

我有以下 wsdl:

http://www.cmicdataservices.com/datacenter/service.asmx?wsdl

它需要身份验证。查看 WSDL 描述,我没有看到任何方法将身份验证对象、用户名和密码作为参数。我使用 Netbeans 为 WSDL 生成了 jax-ws 源。然而,我不知道在那之后该怎么做。

使用 soapui 我可以连接到网络服务并运行所有方法。但再一次,我想将其构建到一个无需我交互即可运行的客户端。

我的问题在于弄清楚如何使用此生成的代码,netbeans.tv 似乎有一个视频(netbeans soapui 插件视频 2),此后丢失了。有谁知道任何教程或知道如何使用此生成的代码访问 Web 服务的任何示例?

所以我有一个方法 CheckifAuthorized()

在 soapui 中运行我得到以下 xml

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:cmic="http://www.cmicdataservices.com/">
   <soap:Header>
      <cmic:Authentication>
         <!--Optional:-->
         <cmic:UserName>username</cmic:UserName>
         <!--Optional:-->
         <cmic:Password>password</cmic:Password>
      </cmic:Authentication>
   </soap:Header>
   <soap:Body>
      <cmic:CheckIfAuthorized/>
   </soap:Body>
</soap:Envelope>

然后我可以在 soap ui 中运行该请求并返回身份验证成功的响应。

使用 netbeans 和 soapui 生成的 jax-ws 代码,我有以下内容:

package javaapplication7;

/**
 *
 * @author grant
 */
public class Main {

    public static void main(String[] args) {

        Boolean result = checkIfAuthorized();
        System.out.println("The result is: " + result);
    }

    private static boolean checkIfAuthorized() {
        javaapplication7.CMICDatacenterService service = new javaapplication7.CMICDatacenterService();
        javaapplication7.CMICDatacenterServiceSoap port = service.getCMICDatacenterServiceSoap();
        return port.checkIfAuthorized();
    }
}

这将失败并出现以下错误

run:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Server was unable to process request. ---> Object reference not set to an instance of an object.
        at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
        at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)
        at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
        at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
        at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
        at $Proxy30.checkIfAuthorized(Unknown Source)
        at javaapplication7.Main.checkIfAuthorized(Main.java:24)
        at javaapplication7.Main.main(Main.java:17)
Java Result: 1

这与我在尝试使用 python 进行服务时遇到的问题相同。从那以后,我选择了使用 Java,因为我觉得我在解析 xml 和创建对象时可能会有更快的周转时间,因为我已经为此创建了实体。

谢谢。

授予

我不想回答这个问题,因为我仍然想弄清楚我能在这里做什么,但我最终还是手写了以下请求。现在我可以将它转换成一个 xml 对象并按照我的方式进行,但我想 soapui 使所有这一切变得更加容易。我真正不明白的是如何使用soapui构建这个请求并将其合并到我的项目中:

public class Main {

    public final static String DEFAULT_SERVER =
            "http://www.cmicdataservices.com/datacenter/service.asmx";
    public final static String SOAP_ACTION =
            "http://www.cmicdataservices.com/CheckIfAuthorized";

     public static void main(String[] args) {
         String server = DEFAULT_SERVER;
         String UserName = "Username";
         String Password="Password";


     try{
             URL url = new URL(server);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            connection.setRequestProperty("Host", "www.cmicdataservices.com");
            OutputStream out = connection.getOutputStream();
            Writer wout = new OutputStreamWriter(out);
            // Uncomment the following and comment out the previous two lines to see your xml
            //BufferedWriter wout = new BufferedWriter(new FileWriter("/tmp/testXML.xml"));

            //Start writing soap request - Envelope
            wout.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
            wout.write("<soap12:Envelope ");
            wout.write("xmlns:xsi=");
            wout.write("'http://www.w3.org/2001/XMLSchema-instance' ");
            wout.write("xmlns:xsd=");
            wout.write("'http://www.w3.org/2001/XMLSchema' ");
            wout.write("xmlns:soap12=");
            wout.write("'http://www.w3.org/2003/05/soap-envelope'>\r\n");

            //Soap request header start
            wout.write("<soap12:Header>\r\n");

            //Start writing soap request - Authentication
            wout.write("<Authentication xmlns=");
            wout.write("'http://www.cmicdataservices.com/'>\r\n");
            wout.write("<UserName>" + UserName + "</UserName>\r\n");
            wout.write("<Password>" + Password + "</Password>\r\n");
            // End Authentication
            wout.write("</Authentication>\r\n");

            //End the header
            wout.write("</soap12:Header>\r\n");

            //Start writing the body
            wout.write("<soap12:Body>");
            wout.write("<GetCurrentDataVer1 xmlns=");
            wout.write("'http://www.cmicdataservices.com/' />\r\n");
            // End the Body
            wout.write("</soap12:Body>\r\n");

            // End the Envelope
            wout.write("</soap12:Envelope>\r\n");

            wout.flush();
            wout.close();


            //BufferedWriter fout = new BufferedWriter(new FileWriter("/tmp/testXMLResponse.xml"));
            InputStream in = connection.getInputStream();
            createFile(in, "/tmp/testXMLResponse.xml");
     }
     catch (IOException e) {
         System.err.println(e);
     }
     }

     public static void createFile(InputStream io, String fileName) throws IOException {
         FileOutputStream fout = new FileOutputStream(fileName);
         byte[] buf = new byte[256];
         int read = 0;
         while ((read = io.read(buf)) != -1){
             fout.write(buf, 0, read);
         }
     }

最佳答案

您的代码存在的问题是 SOAP header 中缺少 Authentication 元素。查看 WSDL,它应该始终存在:

<wsdl:operation name="CheckIfAuthorized">
    <soap:operation soapAction="http://www.cmicdataservices.com/CheckIfAuthorized" style="document"/>
    <wsdl:input>
        <soap:body use="literal"/>
        <soap:header message="tns:CheckIfAuthorizedAuthentication" part="Authentication" use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

当 CheckIfAuthorized 请求中没有 Authentication 元素时,您的服务器因不正确的 XML 而失败并出现异常。这里有一个 Python 的示例客户端来演示解决问题的思路,我认为你将它转换为 Java 不是问题。

from suds.client import Client

client = Client("http://www.cmicdataservices.com/datacenter/service.asmx?wsdl")
auth = client.factory.create('Authentication')
auth.UserName = "username"
auth.Password = "password"
client.set_options(soapheaders=auth)
client.service.CheckIfAuthorized()

关于java - soap ui 生成的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5543570/

有关java - soap ui 生成的代码的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  4. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  5. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  6. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  7. ruby-on-rails - 浏览 Ruby 源代码 - 2

    我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru

  8. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  9. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  10. ruby-on-rails - Ruby on Rails - 为文本区域和图片生成列 - 2

    我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数

随机推荐