Hessian的入门

来源:互联网 发布:linux安装discuz论坛 编辑:程序博客网 时间:2024/06/08 14:44

jar包hessian 4.0.38

一、基于servlet的配置

1、需要创建一个接口

package com.yaoxun.spring4.hessian;/** * 测试Hessian服务接口 * @author Loren * */public interface HelloService {/** * 说 * @param name */public void sayHello(String name);}

2、编写实现类

package com.yaoxun.spring4.hessian.impl;import com.yaoxun.spring4.hessian.HelloService;/** * HelloSerivce实现类 * @author Loren * */public class HelloServiceImpl implements HelloService {@Overridepublic void sayHello(String name) {System.out.println("Hello " + name + "!");}}

3、配置web.xml文件

<!-- Hessian配置 --><servlet><servlet-name>hessian-service</servlet-name><servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class><init-param><!-- 服务接口的实现类 --><param-name>home-class</param-name><param-value>com.yaoxun.spring4.hessian.impl.HelloServiceImpl</param-value></init-param><init-param><!-- 服务接口 --><param-name>home-api</param-name><param-value>com.yaoxun.spring4.hessian.HelloService</param-value></init-param></servlet><servlet-mapping>         <servlet-name>hessian-service</servlet-name>         <url-pattern>/hessian</url-pattern>    </servlet-mapping>


4、启动服务器访问

http://localhost:8080/spring4/hessian

出现

Hessian Requires POST

说明配置成功


5、客户端代码

package com.yaoxun.spring4.hessian.test;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;import com.yaoxun.spring4.hessian.HelloService;/** * Hessian测试 * @author Loren * */public class HessianTest {public static void main(String[] args) throws MalformedURLException {String url = "http://localhost:8080/spring4/hessian";HessianProxyFactory factory = new HessianProxyFactory();HelloService helloService = (HelloService) factory.create(HelloService.class,url);helloService.sayHello("张三");}}

二、与springMVC进行整合

1、编写接口和实现类,与servlet方式类似

2、配置web.xml文件

<!-- springmvc的配置 --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- hessian的配置 --><servlet><servlet-name>hessian</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:hessian/spring-hessian.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>hessian</servlet-name><url-pattern>*.h</url-pattern></servlet-mapping>

3、配置springmvc.xml用于springmvc的访问拦截

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><context:component-scan base-package="com.yaoxun.spring4.web.handler"></context:component-scan><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean><!-- 配置视图解析器 要求将jstl的包加到classpath --><!-- ViewResolver --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>

4、配置spring-hessian.xml用于hessian提供服务

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="helloService" class="com.yaoxun.spring4.hessian.impl.HelloServiceImpl"></bean><bean name="/hello.h" class="org.springframework.remoting.caucho.HessianServiceExporter"><property name="service" ref="helloService"></property><property name="serviceInterface" value="com.yaoxun.spring4.hessian.HelloService"></property></bean></beans>

启动服务器,请求

http://localhost:8080/spring4/hello.h

出现:

HTTP Status 405 - HessianServiceExporter only supports POST requests

表示配置成功

-------------------------------------------------------------------------以下代码是他人编写的------------------------------------------------------------------------------------------------

6、客户端代码

hessian-client.xml

<?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4     xsi:schemaLocation="http://www.springframework.org/schema/beans  5         http://www.springframework.org/schema/beans/spring-beans.xsd 6         http://www.springframework.org/schema/context  7         http://www.springframework.org/schema/context/spring-context.xsd"> 8  9     <bean id="hessianClient"10         class="org.springframework.remoting.caucho.HessianProxyFactoryBean">11         <property name="serviceUrl">12             <value>http://localhost:8080/spring-mvc4-rest/hessian/service</value>13         </property>14         <property name="serviceInterface">15             <value>com.cnblogs.yjmyzz.service.hessian.HelloService</value>16         </property>17     </bean>18 19 </beans>

调用示例

 package com.cnblogs.yjmyzz.test; 2 import java.net.MalformedURLException; 3  4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7  8 import com.cnblogs.yjmyzz.service.hessian.HelloService; 9 10 public class HessianServiceTest {    11     @SuppressWarnings("resource")12     @Test13     public void testService() throws MalformedURLException {14         ApplicationContext context = new ClassPathXmlApplicationContext(15                 "hessian-client.xml");16         HelloService hello = (HelloService) context.getBean("hessianClient");17         System.out.println(hello.helloWorld("jimmy.yang"));18     }19 }











0 0
原创粉丝点击