hessian是一个轻量级的二进制webservices
1.自定义对像必须要实现Serializable接口, 这样才可被序列/反序列,要不能会抛异常
2.一个接口
3.实现自定义的接口
4. 在web.xml文件中配置hessian
5.如果实现类实现类自定义的接口 并从HessianServlet继承。
那么在web.xml文件中既可以这样配置
6.编写客户端
7.hessian在处理大量数据的时候性能很低
1.自定义对像必须要实现Serializable接口, 这样才可被序列/反序列,要不能会抛异常
public class Book implements Serializable{
private int id;
private String name;
private double price;
// getters and setters......
}
2.一个接口
public interface Service {
public String sayHello();
public List getAllBooks();
public Book getBook();
}
3.实现自定义的接口
public class ServiceImpl implements Service {
public String sayHello() {
return "this is a simple test";
}
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<Book>();
for (int i = 0; i < 30; i++) {
Book book = new Book();
book.setId(i);
book.setName("name_" + i);
book.setPrice(20 * i);
bookList.add(book);
}
return bookList;
}
public Book getBook() {
Book book = new Book();
book.setId(1);
book.setName("book_name");
book.setPrice(123.0);
return book;
}
}
4. 在web.xml文件中配置hessian
<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.test.hessian.ServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
5.如果实现类实现类自定义的接口 并从HessianServlet继承。
public class WSServlet extends HessianServlet implements Service {
public String sayHello() {
return "this is a simple test";
}
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<Book>();
for (int i = 0; i < 30; i++) {
Book book = new Book();
book.setId(i);
book.setName("name_" + i);
book.setPrice(20 * i);
bookList.add(book);
}
return bookList;
}
public Book getBook() {
Book book = new Book();
book.setId(1);
book.setName("book_name");
book.setPrice(123.0);
return book;
}
}
那么在web.xml文件中既可以这样配置
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>
com.test.hessian.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
6.编写客户端
public class Client {
@SuppressWarnings("unchecked")
public static void main(String args[]){
//发布的服务的url
String url="http://localhost:8080/hessian/hello";
//生成工厂
HessianProxyFactory factory = new HessianProxyFactory();
try {
//生成服务对像
Service service = (Service)factory.create(Service.class, url);
System.out.println(service.sayHello());
//调用java对像
Book book = service.getBook();
System.out.println(book.getName());
long startTime = System.currentTimeMillis();
List<Book> books = service.getAllBooks();
int length = books.size();
for(int i=0; i<length; i++){
Book b = books.get(i);
System.out.println("id="+b.getId()+",name="+b.getName()+",price="+b.getPrice());
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
7.hessian在处理大量数据的时候性能很低