`
sisi1984117
  • 浏览: 156258 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

【转】JAVA和NET交互

阅读更多

  http://wiki.apache.org/ws/FrontPage/Axis/DotNetInterop

    

Interoperability Notes on Apache Axis 1.1 and Microsoft .NET Framework 1.0/1.1 FAQ

Data Types

Q: What datatypes can be safely used between Java and the Microsoft .NET Framework?

A: The following simple Java datatypes can be used: String, boolean, byte, short, int, long, float, and double. You can also create typed arrays of any of the above. Standard Sun JavaBeans and arrays of JavaBeans are supported as well.

Q: What about nested complex types, like a JavaBean that contains other beans, which contain arrays of beans, and so on. Does this work?

A: Yes.

Q: What about transferring java.util.Calendar values?

A: This works, although there are some things you have to look out for. Under Java, the Calendar class includes timezone information. Under .NET, the System.DateTime structure does not contain timezone information. The .NET Framework assumes its timezone is the current timezone when serializing and ignores it when deserializing. As a result, values can be off by +/- 24 hours.

Previously, this page has advised that one possible workaround is to assume any time or date to be UTC (i.e. use ToUniversalTime() method on the .NET side when sending times or dates, and correctly parse the time/date strings originating from the Java side). However, such an approach will not work due to the way .NET serializes the DateTime object. The .NET XML serializer always assumes that the DateTime values being serialized represent local machine time, so it applies the machine local time zone offset as the offset portion of the encoded XML. See this article for more details.

Because the .NET XML serializer encodes the DateTime correctly *only* if the DateTime represents the local machine time, the easiest approach is to send the local time in the DateTime, and send the time zone/offset along with it in a separate variable.

Another possible workaround is to specify the value in WSDL as type xsd:date, which does not carry a timezone. This works only if you use WSDL First (more about that below).

Q: But java.util.Calendar can take the value null, while .NET's System.DateTime cannot. Isn't this a problem?

A: Yes. AXIS will serialize a null java.util.Calendar as something like <DateValue xsi:nil='true'/>. De-serializing this XML into a System.DateTime on the .NET side will result in an exception, since System.DateTime can never be nil (not true with .NET 1.1.4322: .NET translates nil dateTime to .NET default DateTime, 0001-01-01T00:00:00). There are some possible workarounds:

  1. select a particular date value to represent "no date at all", for example 1970/01/01 (or, even better, 0001-01-01). This requires strict enforcement on the Java side so that no nil value is every serialized to the wire (not needed if your .NET version is handling nil dateTime correctly). Also every Java app that uses a Calendar value obtained from a webservice must check the value of the Calendar against this well known "no value" value (this can be solved by adding a custom Axis deserializer that translates 0001-01-01T00:00:00 dateTime to null Calendar using a typeMapping configuration. TODO: add code and configuration sample).

 

b. Wrap every date value in a wrapper type, that could take the null value. Again, this requires that any Java app that sends dates or times only sends them wrapped within such a structure. .NET Can de-serialize null wrapper types. One project that helps in this area is the NullableTypes project on SF.NET.

c. Do special handling of nil DateTime values on the .NET side, as per here.

d. Wait for .NET 2.0, which has support for Nullable'Types built in.

Q: Can you provide mappings for Java datatypes to their equivalents under .NET?

A: Sure.

 

Q: Can the standard Java primitive wrappers like java.lang.Integer or java.lang.Double be used?

A: Sure, why not? Here again, though, you need to concern yourself with null values. Either use one of the workarounds mentioned above in the answer for null java.util.Calendar values, or, don't use the wrapper types, use the value types like int, float, and so on.

Q: What datatypes should I avoid when seeking maximum interoperability?

A: You should avoid the following constructs:

 

      * Standard Java Collection classes.  
      * Typesafe enumerations.  Use static final variables within Java instead. 
      * Multi-dimensional and jagged arrays. 
      * Sparse arrays (allowed in SOAP 1.1, not in SOAP 1.2). 
      * The Java char datatype is not supported because of an omission in XML Schema. 
      * Avoid using the same method name multiple times with varying parameters on a web service. 

 

 

You can avoid these pitfalls automatically by starting with WSDL First.

 


 

Aproach

Q: What design patterns should I use when seeking maximum interoperability?

A: For interop, the best advice is to start with WSDL First. This means you begin designing the Web service by writing WSDL and W3C XML Schema, rather than a Java class or interface. This may feel foreign, but it does pay off in better interop. Rather than specifying a webservice interface in terms of the Java types that are serialized to XML, or de-serialized from XML, WSDL First means you specify the interface in terms of the W3C XML Schema datatypes: xsd:int, xsd:float, xsd:string, xsd:date, and so on, and complexTypes composed of those primitives.

This has the effect of eliminating many interop problems before they start. It also encourages the designer of the web service interface to think in terms of message exchanges, rather than object exchanges.

Q: What if I have a large set of Java classes that I want to use in a webservices interface? I don't want to generate them all from WSDL. Can I still use the WSDL First approach?

A: Well, no. But you can still be interoperable. To do: finish this

 


 

Other Features

Q: How does one go about transmitting attachments between Java and the Microsoft .NET Framework?

A: The base versions of the .NET Framework 1.0 and 1.1 do NOT provide support for attachments.

The Microsoft Web Services Enhancements (WSE) adds support for [/Dime DIME (Direct Internet Message Encapsulation)]. As a result, DIME support requires the installation on WSE on client machines.

Axis supports both MIME and DIME attachments: the server handles either, the client needs to be told to use DIME when sending messages.

Q: Are there any requirements for using Axis with Microsoft Web Services Enhancements (WSE)?

A: To use the WSE toolkit with Axis, you need to turn off WS-Routing.

This can be done by adding the following call:

 

_server.RequestSoapContext.Path.MustUnderstand = false;

 

 

Otherwise you get a MustUnderstand fault when Axis fails to understand the WS-Routing headers. For more info, see Java & .NET Interop - How To.

Q: Is there built in support for compressing messages using something like GZIP?

A: Unfortunately at this time no. In fact, the .NET Framework does not include a built in compression library.

     

    Click on the following link for a discussion and potential work around to the issue: archive It does appear GZIP compression support will be added in the .NET Framework 2.0. See the following article for more information:

    New Features for Web Service Developers in Beta 1 of the .NET Framework 2.0

     


     

    Examples

    Q: Are there examples of JavaBean serialization and access from a C# or other .Net client?

    A: Yes. For a working example see: http://dinoch.dyndns.org:7070/axis1.2/AboutCtService.jsp . It is an AXIS server (v1.2RC3), that exchanges complex types JavaBeans with a C# client. The source is available. This example follows the WSDL First approach - start with WSDL, then generate the server-side and client-side artifacts from that WSDL.

    Q: What about examples of Arrays? or an AXIS Server with a VB.NET client?

    A: Yes. For a working example see: http://dinoch.dyndns.org:7070/axis1.2/AboutArrays2.jsp . It is an AXIS server 1.2RC3, and there are VB.NET and C# clients. The source is available. For more on Array Interop, see DotNetInteropArrays.

    Q: What about very basic examples? Just the primitives. And VB.NET clients?

    A: Ok, see: http://dinoch.dyndns.org:7070/axis1.2/AboutEchoService.jsp . It is an AXIS server 1.2RC3, and there are VB.NET and C# clients. The source is available, with a makefile. It echos strings, ints, floats.

     


     

    Misc

    Q: Are there any documented web service bugs in Microsoft .NET Framework?

    A: Yes, within .NET 1.0 there is an issue with empty array deserialization described in the following article:

    • BUG: Incorrect Results Occur When a Web Service Returns an Array of Size 0

     

    archive

    • This bug has been corrected in the .NET 1.1 release. Starting with Axis 1.1RC2, you can place the following entry within your WSDD file to work around the issue:

     

     

      <globalConfiguration> 
         <parameter name="axis.sendMinimizedElements" value="false"/> 
       </globalConfiguration> 

     

     

    It is recommended that you only enable this option if you must support .NET 1.0 clients. }}}

    Q: What about issues present in AXIS that affect interop?

    A: Yes, be aware of Jira AXIS-1366 , where AXIS publishes a WSDL that does not conform to the actual interface. This is still present in V1.2rC3. The workaround is to build the client from the static WSDL (not the generated WSDL).

    Q: Can you provide a recommendation of how to transport a java.util.Map to C#?

    A: See DotNetInteropMapInfo.

    Q: Is there a list of web service related changes made between the Microsoft .NET Framework 1.0 and 1.1?

    A: Yes, See the following articles:

    XML & Web Services Changes in .NET Framework 1.1

    Changes in System.web.services from .NET Framework 1.0 to 1.1

     


     

    Resources

    MSDN Web Services Developer Center

    West Wind Technologies White Papers

    Q: Why sendNull parameter of the method "org.apache.axis.encoding.SerializationContextImpl.serialize" is hard code set to true ?

    Remark: When you want to access a Microsoft Web Services, the parameters with a Null value shouldn't be sent. I think it will be interesting to add a Tag in the client-config.wsdd to choose if we want to send <Parameter xsi:nil="true"/> in the SOAP envelope of the request.

    分享到:
    评论

    相关推荐

      java与html交互

      在早期Web开发领域,Java Applet作为客户端插件被广泛应用于浏览器环境中,实现更丰富的交互效果和功能。本文将详细介绍如何利用JavaScript与Java Applet之间的交互来获取用户在Internet Explorer(IE)浏览器中的IP...

      android java和js交互

      在android的开发过程中,有很多时候需要用到本地java代码和javascript进行交互。android对交互进行了很好的封装,在开发中我们可以很简单的用java代码调用webview中的js,也可以用webview中的js来调用本地的java代码...

      Android开发java和js交互

      - 性能:大量使用JavaScript和Java交互可能影响性能,应尽量减少不必要的通信。 - 生命周期:在Activity的生命周期方法中,记得正确处理WebView,例如在onResume加载页面,在onPause暂停加载,在onDestroy清除对象。...

      Java和JavaScript库进行交互

      Java和JavaScript库之间的交互是软件开发中的一个重要主题,特别是在构建多平台应用或增强网页功能时。Java和JavaScript作为两种不同的编程语言,它们各自在特定领域有着优势:Java以其强大的后端处理能力,而...

      在Qt平台C++和Java代码的交互

      首先,理解C++与Java交互的基本原理至关重要。这种交互通常通过JNI(Java Native Interface)来实现,它允许Java代码调用本地(如C++)代码,反之亦然。JNI提供了一种标准的接口,使得开发者可以构建可与Java虚拟机...

      Flex和java交互搭建

      Flex和Java交互搭建是开发富互联网应用程序(RIA)时常用的一种技术组合,它允许前端的Flex界面与后端的Java服务器进行数据交换和业务逻辑处理。这个项目新手用来练手非常合适,因为其结构简单,易于理解。接下来,...

      Java与Excel的交互资料

      Java与Excel的交互是Java开发中的一个重要应用领域,特别是在数据处理和报告生成方面。Java提供了多种库,如Apache POI和jxl,使得开发者能够轻松地读取、写入和修改Excel文件。以下是对给定资源中涉及的知识点的...

      java与R交互

      Java与R语言的交互是现代数据科学和软件开发中的一项重要技术。这两种语言各有优势,Java以其强大的企业级应用能力和跨平台性而著名,而R则在统计分析和数据可视化领域独占鳌头。本资源旨在帮助开发者了解如何在Java...

      Flex与java和数据库交互的完美文档

      Flex与java和数据库交互的完美文档

      java前端交互

      在这个过程中,Java通常作为后端语言处理业务逻辑,而前端则负责展示和用户交互。以下是对给定文件信息的详细解读: 1. **jar包**: `imclient_pafa.jar` 这个文件很可能是一个Java应用程序或库的归档文件,用于在...

      FLASH与JAVA的交互

      当谈到“Flash与Java的交互”,我们主要关注的是如何在Flash内容与Java应用程序之间建立通信桥梁,实现数据交换和功能调用。 1. **基础知识**: - Flash:Adobe Flash是一款创建互动内容、动画和应用程序的软件,...

      java+js交互Demo

      Java和JavaScript交互是一种常见的技术实践,特别是在开发单页应用程序(SPA)或富互联网应用程序时。这两种语言虽然在用途上有所不同,但通过特定的技术手段可以实现跨域通信,从而协同工作。下面将详细介绍Java和...

      Java服务端和客户端交互实例

      在Java编程中,服务器端和客户端的交互是网络应用程序的核心部分。这个实例展示了如何通过Java实现这一过程。在这个实例中,我们有两个主要组件:服务器端(Server)和客户端(Client)。下面将详细介绍这两个组件...

      Flex与Java的交互

      Flex与Java的交互是跨平台应用开发中的常见技术组合,允许前端用户界面(UI)与后端业务逻辑进行高效沟通。在本文中,我们将深入探讨如何使用Flex 4与Java进行通信,并通过三种不同的方法实现这一目标:RemoteObject...

      java与dll的交互

      本文将深入探讨Java如何调用DLL中的函数,并详细讲解如何编写和使用DLL,以及解决跨语言交互中可能出现的问题。 1. **Java调用DLL的基础知识** - Java Native Interface (JNI):Java提供了JNI框架,使得Java代码...

      java和dll交互代码

      Java 和 DLL 交互是软件开发中的一个重要话题,尤其是在跨平台应用程序设计中。DLL(Dynamic Link Library)是微软 Windows 操作系统中的动态链接库文件,它包含了一组可重用的函数和数据。Java 作为跨平台的编程...

      Java与JS交互

      最近工作中为了赶进度,在项目中使用了WebView组件,用到了html与java的数据交互,也就是javascript与java的数据交互,本文即介绍了在应用中添加WebView,以及js如何与java进行数据的交互。

      Java3d交互式三维图形编程

      附带的"Java3d交互式三维图形编程.pdf"文档应该包含了详细的教程内容和实例代码,阅读时可结合"SRC"目录下的源代码进行实践。源代码是理解理论知识的关键,通过实际运行和修改代码,你可以更好地理解和掌握Java3D的...

      Rust与Java进行交互实例代码

      Rust与Java进行交互实例代码,使用JNI技术,实现Rust与JAVA的相互调用,从而提高执行效率,本资源是文章《【一起学Rust | 进阶篇 | jni库】JNI实现Java与Rust进行交互》的配套案例代码,供读者进行学习,查看,以...

      张杰Java3d交互式三维图形编程 ,带章节书签,和配套源代码

      《张杰Java3d交互式三维图形编程》是一本深入探讨Java 3D技术的书籍,专注于使用Java语言实现交互式三维图形的应用开发。书中详细介绍了如何利用Java 3D API来创建逼真的三维场景,为读者提供了一个全面了解和实践...

    Global site tag (gtag.js) - Google Analytics