欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hessian序列化

程序员文章站 2024-03-24 11:59:04
...

背景:

之前一篇文章介绍了java的序列化,http://blog.csdn.net/bohu83/article/details/51124079

在java的序列化里面也是介绍rpc框架时候,在远程调用中,需要把参数和返回值通过网络传输,这个使用就要用到序列化将对象转变成字节流,从一端到另一端之后再反序列化回来变成对象。作用了说了,今天在看文章的时候发现别人贴出效率对比,hessian比java的序列化高出很多。特意补充下相关知识点。

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

hessian序列化

hessian序列化比Java序列化高效很多,而且生成的字节流也要短很多。

测试的demo:客户端通过Hessian***协议序列化对象,通过Http Post方式提交到服务器端。

  1. package hessian;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.util.Date;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.client.methods.CloseableHttpResponse;  
  8. import org.apache.http.client.methods.HttpPost;  
  9. import org.apache.http.entity.ByteArrayEntity;  
  10. import org.apache.http.entity.ContentType;  
  11. import org.apache.http.impl.client.CloseableHttpClient;  
  12. import org.apache.http.impl.client.HttpClients;  
  13.        
  14.        
  15.   
  16.     import com.caucho.hessian.io.Hessian2Input;  
  17. import com.caucho.hessian.io.Hessian2Output;  
  18.        
  19.     public class Test {  
  20.        
  21.         public static String urlName = “http://localhost:8080/springmvc-chapter6/hello”;  
  22.        
  23.         public static void main(String[] args) throws Throwable {  
  24.        
  25.             // 序列化  
  26.             ByteArrayOutputStream os = new ByteArrayOutputStream();  
  27.             Hessian2Output h2o = new Hessian2Output(os);  
  28.        
  29.             h2o.startMessage();  
  30.             h2o.writeObject(getPerson());  
  31.             h2o.writeString(”I am client.”);  
  32.             h2o.completeMessage();  
  33.             h2o.close();  
  34.        
  35.             byte[] buffer = os.toByteArray();  
  36.             os.close();  
  37.        
  38.             ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer,  
  39.                     ContentType.create(”x-application/hessian”“UTF-8”));  
  40.                
  41.             CloseableHttpClient client = HttpClients.createDefault();  
  42.             HttpPost post = new HttpPost(urlName);  
  43.             post.setEntity(byteArrayEntity);  
  44.             CloseableHttpResponse response = client.execute(post);  
  45.        
  46.             System.out.println(”response status:\n”  
  47.                     + response.getStatusLine().getStatusCode());  
  48.             HttpEntity body = response.getEntity();  
  49.             System.out.println(”body:”+body);  
  50. //          InputStream is = body.getContent();  
  51. //          Hessian2Input h2i = new Hessian2Input(is);  
  52. //          h2i.startMessage();  
  53. //     
  54. //          Person person = (Person) h2i.readObject();  
  55. //          System.out.println(“response:\n” + person.toString());  
  56. //          System.out.println(h2i.readString());  
  57. //     
  58. //          h2i.completeMessage();  
  59. //          h2i.close();  
  60. //          is.close();  
  61.         }  
  62.        
  63.         public static Person getPerson() {  
  64.             Person person = new Person();  
  65.             person.setAddress(new String[] { “Beijing”“*”“GuangZhou” });  
  66.             person.setBrithday(new Date());  
  67.             person.setGender(false);  
  68.             person.setHeight(168.5D);  
  69.             person.setId(300);  
  70.             person.setName(”Jack”);  
  71.             person.setPhone(188888888);  
  72.             person.setWeight(55.2F);  
  73.             return person;  
  74.         }  
  75.           
  76. }  
package hessian;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;



    import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

    public class Test {

        public static String urlName = "http://localhost:8080/springmvc-chapter6/hello";

        public static void main(String[] args) throws Throwable {

            // 序列化
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Hessian2Output h2o = new Hessian2Output(os);

            h2o.startMessage();
            h2o.writeObject(getPerson());
            h2o.writeString("I am client.");
            h2o.completeMessage();
            h2o.close();

            byte[] buffer = os.toByteArray();
            os.close();

            ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer,
                    ContentType.create("x-application/hessian", "UTF-8"));

            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(urlName);
            post.setEntity(byteArrayEntity);
            CloseableHttpResponse response = client.execute(post);

            System.out.println("response status:\n"
                    + response.getStatusLine().getStatusCode());
            HttpEntity body = response.getEntity();
            System.out.println("body:"+body);
//          InputStream is = body.getContent();
//          Hessian2Input h2i = new Hessian2Input(is);
//          h2i.startMessage();
//   
//          Person person = (Person) h2i.readObject();
//          System.out.println("response:\n" + person.toString());
//          System.out.println(h2i.readString());
//   
//          h2i.completeMessage();
//          h2i.close();
//          is.close();
        }

        public static Person getPerson() {
            Person person = new Person();
            person.setAddress(new String[] { "Beijing", "*", "GuangZhou" });
            person.setBrithday(new Date());
            person.setGender(false);
            person.setHeight(168.5D);
            person.setId(300);
            person.setName("Jack");
            person.setPhone(188888888);
            person.setWeight(55.2F);
            return person;
        }

}

接受:

  1. package cn.javass.chapter6.web.controller;  
  2.   
  3.   
  4. import hessian.Person;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import javax.servlet.ServletInputStream;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.web.bind.annotation.RequestMapping;  
  14. import org.springframework.web.servlet.ModelAndView;  
  15.   
  16.   
  17.   
  18.   
  19. import com.caucho.hessian.io.Hessian2Input;  
  20.   
  21. //@RequestMapping  
  22. @Controller  
  23. public class HelloWorldController {  
  24.   
  25.     @RequestMapping(value = “/hello”)  
  26.     public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {  
  27.           
  28.           
  29.         // 处理请求  
  30.         ServletInputStream sis = request.getInputStream();  
  31.         Hessian2Input h2i = new Hessian2Input(sis);  
  32.    
  33.         h2i.startMessage();  
  34.         Person person = (Person) h2i.readObject();  
  35.         System.out.println(”receive:\n” + person.toString());  
  36.         System.out.println(h2i.readString());  
  37.         h2i.completeMessage();  
  38.         h2i.close();  
  39.         sis.close();  
  40.           
  41.         return person.toString();  
  42.     }  
  43. }  
package cn.javass.chapter6.web.controller;


import hessian.Person;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;




import com.caucho.hessian.io.Hessian2Input;

//@RequestMapping
@Controller
public class HelloWorldController {

    @RequestMapping(value = "/hello")
    public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {


        // 处理请求
        ServletInputStream sis = request.getInputStream();
        Hessian2Input h2i = new Hessian2Input(sis);

        h2i.startMessage();
        Person person = (Person) h2i.readObject();
        System.out.println("receive:\n" + person.toString());
        System.out.println(h2i.readString());
        h2i.completeMessage();
        h2i.close();
        sis.close();

        return person.toString();
    }
}
  1. package hessian;  
  2.   
  3.   
  4. import java.io.Serializable;  
  5. import java.util.Date;  
  6.   
  7. public class Person implements Serializable {  
  8.   
  9.     private static final long serialVersionUID = -1923645274767028479L;  
  10.       
  11.     private String[] address;  
  12.   
  13.     private Date brithday;   
  14.       
  15.     private boolean gender;  
  16.       
  17.     private double height;  
  18.       
  19.     private int id;  
  20.       
  21.     private String name;  
  22.       
  23.     private int phone;  
  24.       
  25.     private float weight;  
  26.   
  27.       
  28.     public String[] getAddress() {  
  29.         return address;  
  30.     }  
  31.   
  32.     public void setAddress(String[] address) {  
  33.         this.address = address;  
  34.     }  
  35.   
  36.   
  37.     public Date getBrithday() {  
  38.         return brithday;  
  39.     }  
  40.   
  41.     public void setBrithday(Date brithday) {  
  42.         this.brithday = brithday;  
  43.     }  
  44.   
  45.     public boolean isGender() {  
  46.         return gender;  
  47.     }  
  48.   
  49.     public void setGender(boolean gender) {  
  50.         this.gender = gender;  
  51.     }  
  52.   
  53.     public double getHeight() {  
  54.         return height;  
  55.     }  
  56.   
  57.     public void setHeight(double height) {  
  58.         this.height = height;  
  59.     }  
  60.   
  61.     public int getId() {  
  62.         return id;  
  63.     }  
  64.   
  65.     public void setId(int id) {  
  66.         this.id = id;  
  67.     }  
  68.   
  69.     public String getName() {  
  70.         return name;  
  71.     }  
  72.   
  73.     public void setName(String name) {  
  74.         this.name = name;  
  75.     }  
  76.   
  77.     public int getPhone() {  
  78.         return phone;  
  79.     }  
  80.   
  81.     public void setPhone(int phone) {  
  82.         this.phone = phone;  
  83.     }  
  84.   
  85.     public float getWeight() {  
  86.         return weight;  
  87.     }  
  88.   
  89.     public void setWeight(float weight) {  
  90.         this.weight = weight;  
  91.     }  
  92.   
  93.   
  94. }  
package hessian;


import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable {

    private static final long serialVersionUID = -1923645274767028479L;

    private String[] address;

    private Date brithday; 

    private boolean gender;

    private double height;

    private int id;

    private String name;

    private int phone;

    private float weight;


    public String[] getAddress() {
        return address;
    }

    public void setAddress(String[] address) {
        this.address = address;
    }


    public Date getBrithday() {
        return brithday;
    }

    public void setBrithday(Date brithday) {
        this.brithday = brithday;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }


}
运行结果如下:

  1. 2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name ‘chapter6’ processing POST request for [/springmvc-chapter6/hello]  
  2. 2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello  
  3. 2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String cn.javass.chapter6.web.controller.HelloWorldController.helloWorld(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException]  
  4. 2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘helloWorldController’  
  5. receive:  
  6. Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]  
  7. I am client.  
  8. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’  
  9. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’; URL [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp]] in DispatcherServlet with name ‘chapter6’  
  10. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp] in InternalResourceView ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’  
  11. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request  
2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'chapter6' processing POST request for [/springmvc-chapter6/hello]
2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello
2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String cn.javass.chapter6.web.controller.HelloWorldController.helloWorld(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException]
2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldController'
receive:
Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]
I am client.
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'; URL [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp]] in DispatcherServlet with name 'chapter6'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp] in InternalResourceView 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request


总结:

1、序列化对象要实现java.io.Serializable接口。

2、单文本传输没必要使用hessian,pojo适合用hessian,但是要注意序列化的规范,可能特殊类型支持的不够完善。
本文是是初步使用,背后的原理待整理。

参考:

http://aiilive.blog.51cto.com/1925756/1601574

转载自http://blog.csdn.net/bohu83/article/details/51210468

背景:

之前一篇文章介绍了java的序列化,http://blog.csdn.net/bohu83/article/details/51124079

在java的序列化里面也是介绍rpc框架时候,在远程调用中,需要把参数和返回值通过网络传输,这个使用就要用到序列化将对象转变成字节流,从一端到另一端之后再反序列化回来变成对象。作用了说了,今天在看文章的时候发现别人贴出效率对比,hessian比java的序列化高出很多。特意补充下相关知识点。

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

hessian序列化

hessian序列化比Java序列化高效很多,而且生成的字节流也要短很多。

测试的demo:客户端通过Hessian***协议序列化对象,通过Http Post方式提交到服务器端。

  1. package hessian;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.util.Date;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.client.methods.CloseableHttpResponse;  
  8. import org.apache.http.client.methods.HttpPost;  
  9. import org.apache.http.entity.ByteArrayEntity;  
  10. import org.apache.http.entity.ContentType;  
  11. import org.apache.http.impl.client.CloseableHttpClient;  
  12. import org.apache.http.impl.client.HttpClients;  
  13.        
  14.        
  15.   
  16.     import com.caucho.hessian.io.Hessian2Input;  
  17. import com.caucho.hessian.io.Hessian2Output;  
  18.        
  19.     public class Test {  
  20.        
  21.         public static String urlName = “http://localhost:8080/springmvc-chapter6/hello”;  
  22.        
  23.         public static void main(String[] args) throws Throwable {  
  24.        
  25.             // 序列化  
  26.             ByteArrayOutputStream os = new ByteArrayOutputStream();  
  27.             Hessian2Output h2o = new Hessian2Output(os);  
  28.        
  29.             h2o.startMessage();  
  30.             h2o.writeObject(getPerson());  
  31.             h2o.writeString(”I am client.”);  
  32.             h2o.completeMessage();  
  33.             h2o.close();  
  34.        
  35.             byte[] buffer = os.toByteArray();  
  36.             os.close();  
  37.        
  38.             ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer,  
  39.                     ContentType.create(”x-application/hessian”“UTF-8”));  
  40.                
  41.             CloseableHttpClient client = HttpClients.createDefault();  
  42.             HttpPost post = new HttpPost(urlName);  
  43.             post.setEntity(byteArrayEntity);  
  44.             CloseableHttpResponse response = client.execute(post);  
  45.        
  46.             System.out.println(”response status:\n”  
  47.                     + response.getStatusLine().getStatusCode());  
  48.             HttpEntity body = response.getEntity();  
  49.             System.out.println(”body:”+body);  
  50. //          InputStream is = body.getContent();  
  51. //          Hessian2Input h2i = new Hessian2Input(is);  
  52. //          h2i.startMessage();  
  53. //     
  54. //          Person person = (Person) h2i.readObject();  
  55. //          System.out.println(“response:\n” + person.toString());  
  56. //          System.out.println(h2i.readString());  
  57. //     
  58. //          h2i.completeMessage();  
  59. //          h2i.close();  
  60. //          is.close();  
  61.         }  
  62.        
  63.         public static Person getPerson() {  
  64.             Person person = new Person();  
  65.             person.setAddress(new String[] { “Beijing”“*”“GuangZhou” });  
  66.             person.setBrithday(new Date());  
  67.             person.setGender(false);  
  68.             person.setHeight(168.5D);  
  69.             person.setId(300);  
  70.             person.setName(”Jack”);  
  71.             person.setPhone(188888888);  
  72.             person.setWeight(55.2F);  
  73.             return person;  
  74.         }  
  75.           
  76. }  
package hessian;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;



    import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

    public class Test {

        public static String urlName = "http://localhost:8080/springmvc-chapter6/hello";

        public static void main(String[] args) throws Throwable {

            // 序列化
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Hessian2Output h2o = new Hessian2Output(os);

            h2o.startMessage();
            h2o.writeObject(getPerson());
            h2o.writeString("I am client.");
            h2o.completeMessage();
            h2o.close();

            byte[] buffer = os.toByteArray();
            os.close();

            ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer,
                    ContentType.create("x-application/hessian", "UTF-8"));

            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(urlName);
            post.setEntity(byteArrayEntity);
            CloseableHttpResponse response = client.execute(post);

            System.out.println("response status:\n"
                    + response.getStatusLine().getStatusCode());
            HttpEntity body = response.getEntity();
            System.out.println("body:"+body);
//          InputStream is = body.getContent();
//          Hessian2Input h2i = new Hessian2Input(is);
//          h2i.startMessage();
//   
//          Person person = (Person) h2i.readObject();
//          System.out.println("response:\n" + person.toString());
//          System.out.println(h2i.readString());
//   
//          h2i.completeMessage();
//          h2i.close();
//          is.close();
        }

        public static Person getPerson() {
            Person person = new Person();
            person.setAddress(new String[] { "Beijing", "*", "GuangZhou" });
            person.setBrithday(new Date());
            person.setGender(false);
            person.setHeight(168.5D);
            person.setId(300);
            person.setName("Jack");
            person.setPhone(188888888);
            person.setWeight(55.2F);
            return person;
        }

}

接受:

  1. package cn.javass.chapter6.web.controller;  
  2.   
  3.   
  4. import hessian.Person;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import javax.servlet.ServletInputStream;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.web.bind.annotation.RequestMapping;  
  14. import org.springframework.web.servlet.ModelAndView;  
  15.   
  16.   
  17.   
  18.   
  19. import com.caucho.hessian.io.Hessian2Input;  
  20.   
  21. //@RequestMapping  
  22. @Controller  
  23. public class HelloWorldController {  
  24.   
  25.     @RequestMapping(value = “/hello”)  
  26.     public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {  
  27.           
  28.           
  29.         // 处理请求  
  30.         ServletInputStream sis = request.getInputStream();  
  31.         Hessian2Input h2i = new Hessian2Input(sis);  
  32.    
  33.         h2i.startMessage();  
  34.         Person person = (Person) h2i.readObject();  
  35.         System.out.println(”receive:\n” + person.toString());  
  36.         System.out.println(h2i.readString());  
  37.         h2i.completeMessage();  
  38.         h2i.close();  
  39.         sis.close();  
  40.           
  41.         return person.toString();  
  42.     }  
  43. }  
package cn.javass.chapter6.web.controller;


import hessian.Person;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;




import com.caucho.hessian.io.Hessian2Input;

//@RequestMapping
@Controller
public class HelloWorldController {

    @RequestMapping(value = "/hello")
    public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {


        // 处理请求
        ServletInputStream sis = request.getInputStream();
        Hessian2Input h2i = new Hessian2Input(sis);

        h2i.startMessage();
        Person person = (Person) h2i.readObject();
        System.out.println("receive:\n" + person.toString());
        System.out.println(h2i.readString());
        h2i.completeMessage();
        h2i.close();
        sis.close();

        return person.toString();
    }
}
  1. package hessian;  
  2.   
  3.   
  4. import java.io.Serializable;  
  5. import java.util.Date;  
  6.   
  7. public class Person implements Serializable {  
  8.   
  9.     private static final long serialVersionUID = -1923645274767028479L;  
  10.       
  11.     private String[] address;  
  12.   
  13.     private Date brithday;   
  14.       
  15.     private boolean gender;  
  16.       
  17.     private double height;  
  18.       
  19.     private int id;  
  20.       
  21.     private String name;  
  22.       
  23.     private int phone;  
  24.       
  25.     private float weight;  
  26.   
  27.       
  28.     public String[] getAddress() {  
  29.         return address;  
  30.     }  
  31.   
  32.     public void setAddress(String[] address) {  
  33.         this.address = address;  
  34.     }  
  35.   
  36.   
  37.     public Date getBrithday() {  
  38.         return brithday;  
  39.     }  
  40.   
  41.     public void setBrithday(Date brithday) {  
  42.         this.brithday = brithday;  
  43.     }  
  44.   
  45.     public boolean isGender() {  
  46.         return gender;  
  47.     }  
  48.   
  49.     public void setGender(boolean gender) {  
  50.         this.gender = gender;  
  51.     }  
  52.   
  53.     public double getHeight() {  
  54.         return height;  
  55.     }  
  56.   
  57.     public void setHeight(double height) {  
  58.         this.height = height;  
  59.     }  
  60.   
  61.     public int getId() {  
  62.         return id;  
  63.     }  
  64.   
  65.     public void setId(int id) {  
  66.         this.id = id;  
  67.     }  
  68.   
  69.     public String getName() {  
  70.         return name;  
  71.     }  
  72.   
  73.     public void setName(String name) {  
  74.         this.name = name;  
  75.     }  
  76.   
  77.     public int getPhone() {  
  78.         return phone;  
  79.     }  
  80.   
  81.     public void setPhone(int phone) {  
  82.         this.phone = phone;  
  83.     }  
  84.   
  85.     public float getWeight() {  
  86.         return weight;  
  87.     }  
  88.   
  89.     public void setWeight(float weight) {  
  90.         this.weight = weight;  
  91.     }  
  92.   
  93.   
  94. }  
package hessian;


import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable {

    private static final long serialVersionUID = -1923645274767028479L;

    private String[] address;

    private Date brithday; 

    private boolean gender;

    private double height;

    private int id;

    private String name;

    private int phone;

    private float weight;


    public String[] getAddress() {
        return address;
    }

    public void setAddress(String[] address) {
        this.address = address;
    }


    public Date getBrithday() {
        return brithday;
    }

    public void setBrithday(Date brithday) {
        this.brithday = brithday;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }


}
运行结果如下:

  1. 2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name ‘chapter6’ processing POST request for [/springmvc-chapter6/hello]  
  2. 2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello  
  3. 2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String cn.javass.chapter6.web.controller.HelloWorldController.helloWorld(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException]  
  4. 2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘helloWorldController’  
  5. receive:  
  6. Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]  
  7. I am client.  
  8. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’  
  9. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’; URL [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp]] in DispatcherServlet with name ‘chapter6’  
  10. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp] in InternalResourceView ‘Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]’  
  11. 2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request  
2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'chapter6' processing POST request for [/springmvc-chapter6/hello]
2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello
2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String cn.javass.chapter6.web.controller.HelloWorldController.helloWorld(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException]
2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldController'
receive:
Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]
I am client.
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'; URL [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp]] in DispatcherServlet with name 'chapter6'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp] in InternalResourceView 'Person [address=[Beijing, *, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'
2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request


总结:

1、序列化对象要实现java.io.Serializable接口。

2、单文本传输没必要使用hessian,pojo适合用hessian,但是要注意序列化的规范,可能特殊类型支持的不够完善。
本文是是初步使用,背后的原理待整理。

参考:

http://aiilive.blog.51cto.com/1925756/1601574