Archive for May, 2009

May 07 2009

Using different data types between Java and WCF

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

Many C# or Java data types can be described by using XML Schema so that we can use XML Schema to communicate between Java and WCF.

 

  1. Primitive Data Types
  2. Primitive data types such as byte, short, int, long, float, double, boolean, char are almost the same in C# and Java. You can use these data types in WCF Contract normally. But there are some points you must pay attention to:
            using short in Java when the type is byte in C#;
            using int in Java when the type is char in C#.

    Here, I listed the details of primitive data types:

    Java Client Data Type

    C# WCF Data Type

    short

    byte

    short

    short

    int

    int

    long

    long

    float

    float

    double

    double

    Boolean

    Boolean

    int

    char

  3. String
  4. C# has a System.String class which is analogous to the java.lang.String class. In C#, the string class can be written as string or String.

     

  5. Enumeration
  6. WCF sends and receives an enumeration value with its name in the String format. For example, for an enumeration variable (C#) as follows:
    public enum EnumColor
    {
        enumBlack = 0,
        enumWhite = 1,
        enumRed = 2
    }

    The generated code is as follows:
    @XmlType(name = "EnumColor")
    @XmlEnum


    public enum EnumColor {
        @XmlEnumValue("enumBlack")
        ENUM_BLACK("enumBlack"),
        @XmlEnumValue("enumWhite")
        ENUM_WHITE("enumWhite"),
        @XmlEnumValue("enumRed")
        ENUM_RED("enumRed");
        private final String value;
        EnumColor(String v) {
            value = v;
        }
        public String value() {
            return value;
        }
        public static EnumColor fromValue(String v) {
            for (EnumColor c: EnumColor.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }
    }

  7. Complex Types: Structure or Class
  8. [DataContract]
    public class CompositeType {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
            }
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    “[DataContract]” and “[DataMember]” make a class or a structure serializable. If a member variable is declared as a [DataMember], it will be serialized. Then you can write the following code in Java class:
    ObjectFactory of = new ObjectFactory();
    CompositeType composite = of.createCompositeType();
    composite.setBoolValue(true); composite.setStringValue(of.createCompositeTypeStringValue("world"));
    CompositeType returnComposite = port.getDataUsingDataContract(composite);

     

  9. Array, List
  10. The Array or List data types can be used as parameters in WCF Operation Contract and the generated Java code of these two data types is the same. Metro generates a new data type by adding a prefix “ArrayOf”. For example: List and String[] are both translated to ArrayOfstring. This class can be used in Java as follows:
    ArrayOfstring arrayValue = new ArrayOfstring();
    arrayValue.getString().add("This");

     

  11. DateTime
  12. There is a data type named “DateTime” in C#. It describes date, time and time zone information. For the DateTime type, Metro generates Java code with the corresponding type “XMLGregorianCalendar”. You can use the XMLGregorianCalendar type directly. However, Java generally uses “GregorianCalendar” to declare date time variable. Therefore, it is necessary to make a conversion between GregorianCalendar and XMLGregorianCalendar.
    /*Convert GregorianCalendar to XMLGregorianCalendar*/
    public XMLGregorianCalendar convertCalendarToXMLCalendar(GregorianCalendar objGregorianCalendar)
    {
        XMLGregorianCalendar objXMLGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(objGregorianCalendar);
        return objXMLGregorianCalendar;
    }

    /*Convert XMLGregorianCalendar to GregorianCalendar*/
    public GregorianCalendar convertXMLCalendarToCalendar(XMLGregorianCalendar objXMLGregorianCalendar)
    {
        objXMLGregorianCalendar.objXMLGregorianCalendar.toGregorianCalendar();
    }

Download source code

 

Links:
Previous article >>>>: Data types between Java and WCF
Next article >>>>:
WCF & Java Interop series home page: WCF & Java Interop

4 responses so far

May 07 2009

Data types between Java and WCF

Published by Kevin Gao under WCF & Java Interop

This article is a part of WCF & Java Interop

SCM Anywhere is a comprehensive tool and uses various C# data types, including lists. You may doubt whether Java can recognize these C# data types. Actually, WCF uses Web Services Description Language (WSDL) to describe services and uses XML Schema to describe the data types used in Messages. For more information about XML Schema for data types, you can refer to: XML Schema Part 2: Datatypes Second Edition.

 

WSDL is language-independent and supported by Microsoft, IBM, and other vendors. WCF provides a rich infrastructure for exporting, publishing, retrieving, and importing service metadata. WCF services use metadata to describe how to interact with the service’s endpoints so that tools, such as wsimport, can automatically generate client code for accessing the service.

 

C# basic data types can be described in WSDL as a specific format. Metro generates Java client code from the WSDL document. For example, the syntax of the “int” data type is as follows:

<xs:element minOccurs="0" name="value" type="xs:int" />

In all, many data types can be used between Java and WCF. I will talk about some common data types between Java and WCF in the next article.

 

Links:
Previous article >>>>: Java client and WCF server
Next article >>>>: Using different data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop

No responses yet

Version Control Software/System | Source Control Software/System | Software Configuration Management | SCM Hosting Solution | Bug Tracking System
SourceSafe (VSS) Replacement/Alternative | SourceSafe (VSS) Hosting | SourceSafe (VSS) Remote/Web/Internet Access | Scanner COM
Customer Service Software | Live Chat | Live Help | Forum Software | Knowledge Base Software | Newsletter Email Marketing Software