Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据

以下给出demo代码

服务端代码:

配置jar:

<!-- hessian 轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷,采用的是二进制RPC协议 -->
<hessian.version>4.0.51</hessian.version>

<!-- hessian jar 包添加 start -->
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>${hessian.version}</version>
</dependency>
<!-- hessian jar 包添加 end -->
web.xml配置:

<!-- hessian demo start -->
<servlet>
	<servlet-name>hello</servlet-name>
	<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
	<init-param>
		<param-name>service-class</param-name>
		<param-value>com.noteshare.dataInteraction.hessian.services.impl.BasicServiceImpl</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>hello</servlet-name>
	<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- hessian demo end -->
实体类:

package com.noteshare.dataInteraction.hessian.entity;

import java.io.Serializable;

public class User implements Serializable{
	private static final long serialVersionUID = 2368176842478981632L;
	String userName = "snoopy";
	String password = "showme";

	public User(String user, String pwd) {
		this.userName = user;
		this.password = pwd;
	}

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}
}



接口定义

package com.noteshare.dataInteraction.hessian.services;

import com.noteshare.dataInteraction.hessian.entity.User;

public interface BasicService {
	public void setGreeting(String greeting);

	public String hello();

	public User getUser();
}


接口实现

package com.noteshare.dataInteraction.hessian.services.impl;

import com.noteshare.dataInteraction.hessian.entity.User;
import com.noteshare.dataInteraction.hessian.services.BasicService;

public class BasicServiceImpl implements BasicService {
	private String _greeting = "Hello, world";

	public void setGreeting(String greeting) {
		_greeting = greeting;
		System.out.println("set greeting success:" + _greeting);
	}

	public String hello() {
		return _greeting;
	}

	public User getUser() {
		return new User("prance", "meshow");
	}
}


以上是服务端代码,启动tomcat接口即发布成功

客户端代码

接口
package com.noteshare.dataInteraction.hessian.services;

import com.noteshare.dataInteraction.hessian.entity.User;

public interface BasicService {
	public void setGreeting(String greeting);

	public String hello();

	public User getUser();
}



客户端测试类:
package com.noteshare.dataInteraction.hessian.client;

import com.caucho.hessian.client.HessianProxyFactory;
import com.noteshare.dataInteraction.hessian.services.BasicService;

public class BasicClient {
	public static void main(String[] args) throws Exception {
		String url ="http://127.0.0.1:8080/test/hello";
		HessianProxyFactory factory = new HessianProxyFactory();
		BasicService basic = (BasicService) factory.create(BasicService.class, url);
		System.out.println("Hello:" + basic.hello());
		System.out.println("Hello:" + basic.getUser().getUserName());
		System.out.println("Hello:" + basic.getUser().getPassword());
		basic.setGreeting("HelloGreeting");
		System.out.println("Hello:" + basic.hello());
	}
}

输出结果为:
Hello:Hello, world
Hello:prance
Hello:meshow
Hello:HelloGreeting














作者:星辰 时间:2018-04-18 浏览 264评论 0 赞 0砸 0 标签: 数据对接
评论
还可以再输入500个字

请您注意

·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款