惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - rottenapple

我遇到的Sqlserver2005 SSIS 的Bug SSIS导入Oracle中的性能问题 Excel内容不能正确倒入SSIS? 使用Oracle Generic Connectivity连接SqlServer asp.net下的用户权限管理模块设计 msdn也出现这种错误。。。 为Log4Net添加异常处理模块 - rottenapple - 博客园 ibatis.net的配置文件设计 今天发现了ibatis.net的一个bug The Visual Studio template information is out of date. asp.net 设计中的页面流程控制的一点想法(讨论贴) - rottenapple - 博客园 New job,New chance 使用强类型DataSet的好处 如何使应用程序只有一个实例在运行 在Framework1.0下同时连接SqlServer和Oracle的一些体会 VS2002 与 IIS6.0的一个bug T_SQL中的事务处理 SqlCommand的ExecuteNonQuery()方法执行多条T-SQL语句 博客园的Bug
利用WSDL2JAVA实现JAVA调用.Net的Web Services(1)
rottenapple · 2005-01-18 · via 博客园 - rottenapple

声明:本文乃本人源创成果,任何非商业用途的转载请发信通知我一声。

一:前言

    目前,越来越多的系统开始使用Web Services。但是由于Java.Net实现Web Services的不同,如何方便无缝的实现相互之间的调用还是有一定的困难。还好,由于XML的机器可读性以及双方都遵守的WSDL标准,使我们可以利用一个工具实现快速开发。本文将介绍如何使用WSDL2JAVA这个工具来实现.Net Web ServicesJAVA客户端的调用。为了方便开发,本文将使用Eclipse作为开发平台,使用EclipseWSDL2JAVA插件来实现此功能。

二:系统配置

操作系统:Windows2000 +SP4

Java环境:Eclipse2.1

Org.apache.axis.wsdl2java.eclipse_1.1-rc2.0.2.zip

Org.apache.axis_1.1.zip

.Net环境:Visual Studio.Net

三:AddInt方法的实现

    测试最简单的WebServices调用。输入参数和返回结果都是标准数据类型。

31 .NetWebServices的实现

1.新建一个Asp.Net Web Services工程。

2.在Service1.asmx中添加下列代码:

[WebMethod]

public int AddInt(int i, int j)

{

return i+j;

}

3.在Visual Studio.Net中执行此工程,在IE中测试AddInt方法,返回结果正确。这样,我们就配置好了一个.Net Web Services

32 Java客户端的实现

1.解压缩Org.apache.axis.wsdl2java.eclipse_1.1-rc2.0.2.zipOrg.apache.axis_1.1.zip这两个文件至D:/WSDL2JAVA目录。

2.根据D:\WSDL2Java\org.apache.axis.wsdl2java.eclipse_1.1.0.2\html中的Index的说明配置Eclipse

3.在Eclipse中新建一个目录Work,单击鼠标右键,使用Import Web Services添加.Net WebServices的引用,生成Java的客户代理。(具体方法见index文件的说明)

4.在Work目录下新建一个Java文件,命名InvokeNetService.java代码如下:

import chenxuguang.WebServicesTest.*;

public class InvokeNetService {

           Service1Locator _Locator ;

           Service1Soap _localService;

           public InvokeNetService()

           {

              try{

                        _Locator = new Service1Locator();

                        _localService = _Locator.getService1Soap();

}

              catch(Exception any){

                       any.printStackTrace();

               }

           }

           //Call AddInt WebMethod

           public void callAddInt()

           {

                       try{

                                  int i = _localService.addInt(1,2);

                                  System.out.println("1+2 is "+ i);

                       }

                       catch(Exception any)

                       {

                                  any.printStackTrace();

                       }

           }

5.在Wrok目录下新建一个Java文件,命名为InvokeService.java。代码如下:

import chenxuguang.WebServicesTest.*;

public class InvokeService {

           public static void main(String[] args) {

                       InvokeNetService netService = new InvokeNetService();

                       netService.callAddInt();

           }

}

6.运行InvokeService.java。执行结果如下:

1+2 is 3

执行成功!

四:getCustomer()的方法实现

Customer是用于自定义的一个类,用来存放客户信息。本例将讨论在Java中实现.Net中子定义的类。

41 .Net Web Services getCustomer() WebMethod的实现

1.新建一个类,命名为Customer.cs,代码如下:

using System;

namespace WebServiceAdd

{

       [Serializable]

       public class Customer

       {

              public Customer()

              {

                     this.CustomerID = "TestID";

                     this.CustomerName = "TestName";

              }

              public string CustomerID;

              public string CustomerName;

       }

}

2.在Services1.asmx中加入getCustomer() WebMethod。代码如下:

[WebMethod]

public Customer getCustomer(string id)

{

       Customer customer = new Customer();

       return customer;

}

3.在IE中测试getCustomer()返回结果如下:

  <?xml version="1.0" encoding="utf-8" ?>

- <Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://chenxuguang/WebServicesTest/">

  <CustomerID>TestID</CustomerID>

  <CustomerName>TestName</CustomerName>

  </Customer>

结果正确!

42 Java客户端的实现

1.重新生成Java客户端代理。

   执行成功后,可以在项目目录下发现多了三个Java文件,分别是_getCustomer.java, _getCustomerResponse.java, Customer.java。其中,Customer.java就是我们在.Net中子定义的Costomer类的Java实现。

2.在InvokeNetService 中添加下列代码:

public void callCustomerService()

{

           try{

                       _getCustomer param = new _getCustomer();

                       param.setId("chen" );

                       _getCustomerResponse customerRes = this._localService.getCustomer(param);

                       Customer customer = customerRes.getGetCustomerResult();

                       System.out.println("ID is :" + customer.getCustomerID());

                       System.out.println("Name is :" + customer.getCustomerName());

           }

           catch(Exception any)

           {

                       any.printStackTrace();

           }

}

3.修改InvokeServiceMain方法的代码:

           InvokeNetService netService = new InvokeNetService();

           netService.callCustomerService();

4.执行InvokeService,输入结果如下:

ID is :TestID

Name is :TestName

测试成功!

注意事项:

(1)      OOP的角度来说,我们应该封装Customer类,使用Get/Set 属性赋值,而不是使用公共类型的类变量。但是,如果类使用Get/Set属性并在类中修改专有变量,则数据就不会正确的发送,因为没有使用XML描述对象在内存中的二进制表示。

(2)      Web Services的角度来说,系统之间交换的是数据,并且是无状态的。因此,我们的返回结果更应该是一个纯数据类型,而不应该是一个类。我们应该在WebMethod中包装返回结果并以string或者其它的类型传递。而在客户端,关注的也是返回的数据,而不是其中的方法实现。因此,从这个角度看,我们在实现WebMethod的时候返回结果应该是一个纯数据类型(或数据字典),而不应该是一个复杂的类对象。  

太长了,明天再写后面的把。后面将讲一下如何传递XMLDocument类型。