首页 > 代码库 > Java使用Hessian远程方法调用
Java使用Hessian远程方法调用
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency>
/** * Copyright (C),HTF<br> * 服务接口 * * @author muzhongjiang * @date 2014年8月5日 */public interface BasicService { public void setServiceName(String serverName); public String getServiceName(); public User createUser();}
/** * Copyright (C),HTF<br> * 服务实现类 * * @author muzhongjiang * @date 2014年8月5日 */public class BasicServiceImpl implements BasicService { private String serviceName; @Override public void setServiceName(String serverName) { this.serviceName = serverName; } @Override public String getServiceName() { return this.serviceName; } @Override public User createUser() { return new User("zhangsan", "123456"); } }
import java.net.MalformedURLException;import org.junit.Before;import org.junit.Test;import com.caucho.hessian.client.HessianProxyFactory; public class HessianTest { private BasicService basicService; @Before public void init() throws MalformedURLException { HessianProxyFactory factory = new HessianProxyFactory(); String url = "http://localhost:8080/HessianServer/basic"; basicService = (BasicService)factory.create(BasicService.class, url); } @Test public void testBasic() { basicService.setServiceName("BasicService"); System.out.println(basicService.getServiceName()); System.out.println(basicService.createUser().getUsername()); System.out.println(basicService.createUser().getPassword()); } }
import java.io.Serializable;public class User implements Serializable { private static final long serialVersionUID = -7163479978658496547L; private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; }}
web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>basic</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>service-class</param-name> <param-value>com.loujinhe.service.impl.BasicServiceImpl</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>basic</servlet-name> <url-pattern>/basic</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。