Hessian+Maven Demo 一個簡單的例項(有問題,可以提問,經常線上,原始碼會粘上連線,可下載)

common_util發表於2016-07-26

1:這個小Demo是我在IOS系統上搞起來的,MAVEN、JDK、TOMCAT、ECLIPSE安裝都不一一說了,可以自己嘗試一下,有問題,可以線上問我

2:這是一個Hessian入門級別的專案,專案是Maven專案、方便管理和以後整合SpringMVC。


==================================把環境搞起來,先跑一下,就知道Hessian Demo作為一個輕量級的remoting onhttp工具的大概原理了=====

1Maven的Pom.xml檔案  依賴的包(hessian的jar包,會報異常,到時候可以注意一下這個問題

<dependencies>

<dependency>

<groupId>com.caucho</groupId>

<artifactId>hessian</artifactId>

<!--  <version>4.0.7</version> -->

<version>3.1.5</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.0.1</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.1</version>

</dependency>

</dependencies>


2:web.xml(這裡的內容都已經寫好註釋了,不懂的地方可以再看一下)

<!-- 使用Hessian遠端訪問:配置攔截 -->

<servlet>

<servlet-name>synchLocalhost</servlet-name>

<!-- 自定義名稱 -->

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

<!-- 這個是呼叫jar裡面的Hessian攔截器,不需要更改-->

<init-param>

<param-name>home-class</param-name>

<!-- 這個是Hessian攔截器後跳轉的實現類識別符號,不需要更改 -->

<param-value>com.shenhl.hession.HelloHessianImpl</param-value>

<!-- 自定義Hessian的實現類的全路徑 -->

</init-param>

<init-param>

<param-name>home-api</param-name>

<!-- 這個是Hessian攔截器後跳轉的介面識別符號,不需要更改 -->

<param-value>com.shenhl.hession.HelloHessian</param-value>

<!-- 自定義Hessian的介面類的全路徑 -->

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<!-- servlet-name:與前面的一致 -->

<!-- url-pattern :訪問入口名 客戶端通過訪問入口進入伺服器中Hessian攔截器,攔截後跳轉到自定義的實現類中方法處理-->

<servlet-name>synchLocalhost</servlet-name>

<url-pattern>/synchLocalhost</url-pattern>

</servlet-mapping>

3:類,原始碼:HelloHessian.java

package com.shenhl.hession;


import java.util.List;

import java.util.Map;


public interface HelloHessian {

String sayHello();


List myLoveFruit();


Map myBabays();


MyCar getMyCar();

}


4:HelloHessianImpl介面的實現類

package com.shenhl.hession;


import java.io.Serializable;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import com.caucho.hessian.server.HessianServlet;

/**

 * 2016 7 26 9:27

 * @author user

 *

 */

public class HelloHessianImplextends HessianServlet implements HelloHessian , Serializable{


/**

*/

privatestatic finallong serialVersionUID =1L;


public String sayHello() {


return"welcom to my Hessian";

}


public List<String> myLoveFruit() {

List<String> list =new ArrayList<String>();

list.add("blueberry");

list.add("banana");

list.add("grape");

returnlist;

}


public Map<String, String> myBabays() {

Map<String, String> map = new HashMap<String, String>();

map.put("son","張大四");

map.put("daughter","李小三");

map.put("son","王小二");

returnmap;

}


public MyCar getMyCar() {

MyCar car =new MyCar();

car.setCarName("英菲尼迪");

car.setCarModel("Q50");

returncar;

}


}


5:例項類MyCar

package com.shenhl.hession;


import java.io.Serializable;


public class MyCarimplements Serializable {


/**

*/

private static final long serialVersionUID = 5302943492321676322L;


private StringcarName;

private StringcarModel;


public String getCarName() {

returncarName;

}


public void setCarName(String carName) {

this.carName =carName;

}


public String getCarModel() {

returncarModel;

}


public void setCarModel(String carModel) {

this.carModel =carModel;

}


@Override

public String toString() {


return"my car name:[" + this.carName +"] model:[" + this.carModel +"].";

}


}



6:HessianClientTest.java類

package com.shenhl.hession;


import java.net.MalformedURLException;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;


import com.caucho.hessian.client.HessianProxyFactory;


/**

 * 參考的源文:http://ribbonchen.blog.163.com/blog/static/118316505201472024252460/

 * 

 * @author user

 *

 */

public class HessianClientTest {


public static void main(String[] args) {

//URL:http://本地地址:服務埠/專案名稱/web.xml中配置的名稱

String url ="http://localhost:8080/hession_demo/synchLocalhost";

HessianProxyFactory factory = new HessianProxyFactory() ;

try {

HelloHessian hello = (HelloHessian)factory.create(HelloHessian.class,url);

System.out.println(hello.sayHello());

MyCar car =hello.getMyCar();     

            System.out.println(car.toString());     

    

            for (Object entry:hello.myBabays().entrySet()) {

                System.out.println(entry +"************" + entry);     

            }     

    

            for (Object str:hello.myLoveFruit()) {     

                System.out.println("Object Love Fruit:"+str);     

            } 

            Map babies = hello.myBabays();

            Iterator it = babies.entrySet().iterator();

            while(it.hasNext()){

            Entryentry = (Entry)it.next();

            System.out.println(entry.getKey() +"    " + entry.getValue());

            }

    

        } catch (MalformedURLException e) {     

            e.printStackTrace();     

        }   

}


}



相關文章