Java and c # Use hessian for communication

Source: Internet
Author: User

Java and c # Use hessian for communication
Create a communication interface IHello: copy the code package hessian. test. server; import java. util. arrayList; public interface IHello {String sayHello (String msg); void sayHello2 (int bean); void print (String msg); HelloBean getData (HelloBean bean ); arrayList <HelloBean> getBeanList (); ComplexData getComplexData ();} copies an implementation of the IHello interface of the Code: HelloImpl. java copy code package hessian. test. server; import java. util. arrayList; public class HelloImpl Implements IHello {public String sayHello (String msg) {return "Hello" + msg;} public void sayHello2 (int bean) {System. out. println ("Hello" + bean);} public void print (String msg) {System. out. println (msg);} public HelloBean getData (HelloBean bean) {HelloBean result = new HelloBean (); result. setName ("lu xiaoxun a new name"); result. setAge (26); System. out. print (bean. getName (); return result;} p Ublic ArrayList <HelloBean> getBeanList () {ArrayList <HelloBean> beans = new ArrayList <HelloBean> (); HelloBean b1 = new HelloBean (); b1.setName ("lu1 "); b1.setAge (26); beans. add (b1); HelloBean b2 = new HelloBean (); b2.setName ("lu2"); b2.setAge (27); beans. add (b2); return beans;} public ComplexData getComplexData () {ComplexData data = new ComplexData (); ArrayList <HelloBean> beans = getBeanList (); data. SetData (beans, beans. size (); return data ;}copy the Code definition class for data transmission. Both classes must implement the Serializable interface: HelloBean. java copy code package hessian. test. server; import java. io. serializable; public class HelloBean implements Serializable {private static final long serialVersionUID = 570423789882653763L; private String name; private int age; public String getName () {return name ;} public void setName (String name) {this. na Me = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} copy the code ComplexData. java copy code package hessian. test. server; import java. io. serializable; import java. util. arrayList; import java. util. hashMap; import java. util. map; public class ComplexData implements Serializable {private static final long serialVersionUID = 1L; private ArrayList <HelloBean> helloBeans; // privat E Map <String, HelloBean> helloBeanMap; private int number; public int getNumber () {return number;} public ArrayList <HelloBean> getHelloBeans () {return helloBeans ;} public void setData (ArrayList <HelloBean> beans, int num) {this. number = num; this. helloBeans = beans; // helloBeanMap = new HashMap <String, HelloBean> (); // for (HelloBean helloBean: beans) {// if (! HelloBeanMap. containsKey (helloBean. getName () // {// helloBeanMap. put (helloBean. getName (), helloBean); //} copy the code web. xml content: copy the Code <? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <display-name> hessian server </display-name> <servlet-name> hessian </servlet-name> <servlet -Class> com. caucho. hessian. server. hessianServlet </servlet-class> <init-param> <param-name> service-class </param-name> <param-value> hessian. test. server. helloImpl </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> hessian </servlet-name> <url-pattern>/ hessian </url-pattern> </servlet-mapping> </web-app> copy the Hessian client code (c #) define an IHello interface corresponding to the server: IHello. cs copy code public interface IHello {Str Ing sayHello (String msg); void sayHello2 (int bean); void print (String msg); HelloBean getData (HelloBean bean); HelloBean [] getBeanList (); complexData getComplexData ();} copy the Code to define the same communication data class as the server: HelloBean. cs copy the code using System; using System. collections. generic; using System. linq; using System. text; namespace hessian. test. server {public class HelloBean {public String Name {set {name = value;} get {retu Rn name ;}} private String name; // The type and name must be consistent with the public int Age {set {age = value ;}get {return age ;}} on the server ;}} private int age; // The type and Name must be consistent with that on the server. public override String ToString () {return "name:" + Name + "Age:" + age ;}}} copy the code ComplexData. cs: copy the code using System; using System. collections. generic; using System. linq; using System. text; namespace hessian. test. server {public class ComplexData {private He Lobean [] helloBeans; // private Dictionary <String, HelloBean> helloBeanMap; private int number; public int GetNumber () {return number;} public HelloBean [] GetBeans () {return helloBeans;} // public Dictionary <String, HelloBean> GetBeansDic () // {// return helloBeanMap; //} copy the code and add Hessiancsharp to the main project. dll reference. Test code: copy the code using System; using System. collections. generic; using System. linq; using System. text; using hessiancsharp. client; using hessian. test. server; namespace HessianClientTest {class Program {static void Main (string [] args) {string url = @ "http: // localhost: 8080/HessianServerTest/hessian "; CHessianProxyFactory factory = new CHessianProxyFactory (); IHello test = (IHello) factory. create (typeof (IHello ), Url); // Test function Console. writeLine (test. sayHello ("lu"); // print the string test. sayHello2 (12); // print "Hello 12" test on the server console. print ("hessian"); // print "hessian" on the server console // Test Object HelloBean bean = new HelloBean (); // bean. setName ("lu xiaoxun"); bean. name = "luxiaoxun"; HelloBean result = test. getData (bean); Console. writeLine (result. name); Console. writeLine (result. age); Console. writeLine (result ); // Test Object Array HelloBean [] beans = test. getBeanList (); if (beans! = Null) {foreach (HelloBean data in beans) {Console. writeLine (data. toString () ;}// Test complex data ComplexData complexData = test. getComplexData (); if (complexData! = Null) {Console. WriteLine ("Array number:" + complexData. GetNumber (); HelloBean [] comArray = complexData. GetBeans (); if (comArray! = Null) {foreach (HelloBean data in comArray) {Console. writeLine (data. toString () ;}}// Dictionary <String, HelloBean> helloBeanMap = complexData. getBeansDic (); // if (helloBeanMap! = Null) // {// foreach (String key in helloBeanMap. keys) // {// Console. writeLine (helloBeanMap [key]. getHelloBeanInfo (); //} Console. readKey () ;}} Note: 1. The namespace of the object used by the server and the client for data transmission must be consistent. The namespace of the IHello interface can be different between the server and the client, however, the HelloBean and ComplexData used in IHello must be consistent in the namespace of the two HelloBean classes on the Java server and the C # client. 2. The fields of the class must be consistent. The field names and Field Types of the class used for data transmission must be consistent (the modifier types can be different ). 3. serialize classes on the server. 4. Try to use basic data types. From the test above, we can see that there is no problem in passing basic types and there is no problem in passing common class objects, there is no problem when passing the ArrayList (the C # client uses an Array), but there is a problem when passing the HashMap Dictionary. The C # client cannot use the Dictionary to correspond to each other, it may be caused by inconsistent implementation of the hash function. The specific reason is unknown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.