序列化和反序列化(七)——Java对象的网络传输(二)

      《Java对象的网络传输(一)》中分别介绍了如何通过SocketHTTP实现Java对象的网络传输,其实现原理如出一辙——将Java对象转换成二进制进行传输,这种方式要求项目工程必须是JavaJava Web工程,如果要将Java对象状态网络传输至CC#等异构项目,或传至浏览器客户端,进而浏览器客户端解析为JavaScript对象,这种将Java对象转换成二进制进行传输的方式是无法实现的,为了解决这一问题通常将Java对象转换为XMLJson格式,接下来以HTTP协议为例演示如何转换为Json进而实现网络传输:

      keeper Java Web工程:

             UserInfo类:

package com.jd.vo;

public class UserInfo{//没有实现Serializable接口

	private int age;
	private String name;
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

             服务器端:

package com.jd.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jd.vo.UserInfo;

@WebServlet("/DataServlet")
public class DataServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out =  response.getWriter();
		
		UserInfo userInfo = new UserInfo();
		userInfo.setAge(20);
		userInfo.setName("Tom");
		
		String json = new Gson().toJson(userInfo);
		out.write(json);
		out.flush();
		out.close();
	}
}

          venus Java工程:

             UserInfo类:同keeper工程UserInfo

             客户端:

package com.jd.test;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.jd.vo.UserInfo;

public class Test {

	public static void main(String[] args) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/keeper/DataServlet");// 创建http GET请求
			CloseableHttpResponse response = httpclient.execute(httpGet);// 执行请求
			if (response.getStatusLine().getStatusCode() == 200) {// 判断返回状态是否为200
				HttpEntity entity = response.getEntity();
				String json = EntityUtils.toString(entity);
				UserInfo userInfo = new Gson().fromJson(json, UserInfo.class);
				System.out.println(userInfo);
			}
			response.close();
			httpclient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

注意:

       上例中服务器端和客户端创建用于网络传输的Java对象类没有实现Serializable接口;

       为了便于将Java对象转换为Json,第三方开源组织提供了很多jar包:

       fastjson

              https://mvnrepository.com/artifact/com.alibaba/fastjson

       gson:

              https://mvnrepository.com/artifact/com.google.code.gson/gson

       JackJson:

              https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations

              https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core

              https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind

发布了327 篇原创文章 · 获赞 571 · 访问量 164万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览