1.新建maven工程
2.引入jar包
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
3.编写服务端server
package com.nineclient.demo;
import com.nineclient.demo.model.Cat;
public interface BasicService {
Cat sayCat();
String getName();
int getCount();
}
4.编写实现类
package com.nineclient.demo.impl;
import com.nineclient.demo.BasicService;
import com.nineclient.demo.model.Cat;
public class BasicServiceImpl implements BasicService{
@Override
public Cat sayCat() {
return new Cat("Hello kity!");
}
@Override
public String getName() {
return "Hello beer!";
}
@Override
public int getCount() {
return 100;
}
}
5.实现类里面补充一个model
package com.nineclient.demo.model;
import java.io.Serializable;
public class Cat implements Serializable{
private static final long serialVersionUID = 6531696445894455539L;
private String name;
private int age;
public Cat() {}
public Cat(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
6.web.xml配置servlet
<servlet>
<!-- 配置 HessianServlet,Servlet的命名任意-->
<servlet-name>ServiceServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<!-- 配置接口的具体实现类 ,param-name命名任意-->
<init-param>
<param-name>service-class</param-name>
<param-value>com.nineclient.demo.impl.BasicServiceImpl</param-value>
</init-param>
</servlet>
<!-- 映射 HessianServlet的访问URL地址-->
<servlet-mapping>
<servlet-name>ServiceServlet</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
7.启动服务后,在浏览器里面输入http://localhost:8080/hessian/hessian
package com.nineclinet.demo.client;
import java.net.MalformedURLException;
import org.junit.Test;
import com.caucho.hessian.client.HessianProxyFactory;
import com.nineclient.demo.BasicService;
import com.nineclient.demo.model.Cat;
public class HessianHelper {
@Test
public void testBasicService() {
String url = "http://localhost:8080/hessian/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
BasicService service = null;
try {
service = (BasicService)factory.create(BasicService.class, url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String name = service.getName();
int count = service.getCount();
Cat cat =service.sayCat();
System.out.println("name = " + name + "count = " + count + "cat name = " + cat.getName());
}
interface CatInf {
Cat sayCat();
}
@Test
public void testCatInf() {
String url = "http://localhost:8080/hessian/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
CatInf service = null;
try {
service = (CatInf)factory.create(CatInf.class, url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Cat cat =service.sayCat();
System.out.println("cat name = " + cat.getName());
}
interface NameInf {
String getName();
}
@Test
public void testNameInf() {
String url = "http://localhost:8080/hessian/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
NameInf service = null;
try {
service = (NameInf)factory.create(NameInf.class, url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String name =service.getName();
System.out.println("name = " + name);
}
}
9.结果输出
10.确实对您有帮助的话,帮忙点个赞吧