首页 > 代码库 > 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>