Hessian知识学习总结 https://blog.csdn.net/wodediqizhang/article/details/51723821
1 2 3 4 5 6 |
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <!--<version>4.0.7</version>--> <version>4.0.38</version> </dependency> |
公用代码:有必要的时候,可以打包成jar提供给服务端和客户端使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.pandy.test.hessian; import java.io.Serializable; /** * Created by pandy on 16-4-27. */ public class Car implements Serializable { /** * */ private static final long serialVersionUID = -1115598660168001267L; private String name; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the serialversionuid */ public static long getSerialversionuid() { return serialVersionUID; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.pandy.test.hessian; import java.util.List; /** * Created by pandy on 16-4-27. */ public interface IService { /** * @param name * @return */ public String sayHello(String name); /** * @return */ public Car getMyCar(); /** * @return */ public List<String> getList(); } |
服务端:
PandyWebApp-servlet-hessian.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 定义普通bean实例 --> <bean id="serviceImpl" class="com.pandy.demo.hessian.ServiceImpl" /> <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 --> <bean name="/hessian/hessianService.hessian" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- 需要导出的目标bean --> <property name="service" ref="serviceImpl" /> <!-- Hessian服务的接口 --> <property name="serviceInterface" value="com.pandy.demo.hessian.IService" /> </bean> <!-- web.xml --> <!--<servlet-mapping> <servlet-name>PandyWebApp</servlet-name> <url-pattern>*.hessian</url-pattern> </servlet-mapping>--> </beans> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.pandy.demo.hessian; import com.caucho.hessian.server.HessianServlet; import java.util.ArrayList; import java.util.List; /** * Created by pandy on 16-4-27. */ public class ServiceImpl extends HessianServlet implements IService { /** * */ private static final long serialVersionUID = 8385639368192939451L; @Override public String sayHello(String name) { return "hello: " + name; } @Override public Car getMyCar() { Car c = new Car(); c.setName("哈哈车"); return c; } @Override public List<String> getList() { List<String> list = new ArrayList<String>(); list.add("haha"); list.add("hehe"); return list; } } |
客户端:
remoting-client.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8082/hessian/hessianService.hessian</value> </property> <property name="serviceInterface"> <value>com.pandy.test.hessian.IService</value> </property> </bean> </beans> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.pandy.test.hessian; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Created by pandy on 16-4-27. */ public class HessianClient { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("hessian/remoting-client.xml"); IService hello = (IService) context.getBean("myServiceClient"); System.out.println(hello.sayHello("zhuc-spring")); Car c = hello.getMyCar(); System.out.println(c.getName()); List<String> list = hello.getList(); for (String string : list) { System.out.println(string); } } } |