使用java开发SDK你可能会用的4个库

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

使用java开发SDK你可能会用的4个库_第1张图片

使用java开发SDK你可能会用的4个库

  • xstream
  • okhttp
  • fastjson
  • spring boot

xstream

https://github.com/x-stream/xstream/

用法特别简单

定义javabean

package com.xxx.bean;

public class GetAsnStatus {
	private String customerCode;
	private String warehouseCode;
	private String asnCode;
	private String currentPage;
	
	public String getCustomerCode() {
		return customerCode;
	}
	public void setCustomerCode(String customerCode) {
		this.customerCode = customerCode;
	}
	public String getWarehouseCode() {
		return warehouseCode;
	}
	public void setWarehouseCode(String warehouseCode) {
		this.warehouseCode = warehouseCode;
	}
	public String getAsnCode() {
		return asnCode;
	}
	public void setAsnCode(String asnCode) {
		this.asnCode = asnCode;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
}

然后写个简单的测试

package com.xxx.test;

import com.xxx.bean.AsnItem;
import com.xxx.bean.GetAsnStatus;
import com.xxx.bean.Product;
import com.xxx.bean.Sender;
import com.xxx.bean.SyncAsnInfo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TestGetAsnStatus {
	public static void main(String[] args) {
		XStream xstream = new XStream(new DomDriver("utf8"));
		xstream.alias("GetAsnStatus", GetAsnStatus.class);
    
		GetAsnStatus asn = new GetAsnStatus();
		asn.setCustomerCode("TB00002");
		asn.setWarehouseCode("BEST-BJ-001");
		asn.setAsnCode("ASN200909100033445");

		String xml = xstream.toXML(asn);
		System.out.println(xml);
	}
}

返回结果

//		
//		
//		TB00002
//			BEST-BJ-001
//			ASN200909100033445
//		

xstream with cdata

先实现一个Writer

CustomWriter

package com.xxx.core;

import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;

import java.io.Writer;

public class CustomWriter extends PrettyPrintWriter {

    private boolean outCDATA;

    public CustomWriter(Writer writer, NameCoder nameCoder) {
        super(writer, nameCoder);
    }

    @Override
    public void startNode(String name, Class clazz) {
        super.startNode(name, clazz);
        outCDATA = clazz.equals(String.class);
    }

    @Override
    protected void writeText(QuickWriter writer, String text) {
        if (outCDATA) {
            writer.write("");
        } else {
            writer.write(text);
        }
    }
}

说明一下,这里startNode方法是给所有的String类型的value增加cdata,如果有自己的业务,可以在此处判断。

自己实现一个Xpp驱动,返回上面自定的writer

CustomXppDriver

package com.xxx.core;

import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;

import java.io.Writer;

public class CustomXppDriver extends Xpp3Driver {
    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        return new CustomWriter(out, getNameCoder());
    }
}

封装一个简单的工厂

package com.xxx.core;

import com.thoughtworks.xstream.XStream;

public abstract class XStreamFactory {

    public static XStream buildXStream() {
        return new XStream();
    }

    public static XStream buildXStreamCDATA() {
        return new XStream(new CustomXppDriver());
    }
}

buildXStreamCDATA里使用CustomXppDriver,根据上面的代码可知,CustomXppDriver里使用CustomWriter,逻辑就很清晰了

测试一下

注意:依赖xmlpull库,http://www.xmlpull.org/v1/download/需要放到path里

package com.xxx.test;

import java.util.ArrayList;
import java.util.Set;

import com.xxx.bean.Product;
import com.xxx.bean.SyncProductInfo;
import com.xxx.core.XStreamFactory;
import com.xxx.dom.Person;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TestSyncProductInfo {

	public static void main(String[] args) {
		SyncProductInfo info = new SyncProductInfo();
		info.setCustomerCode("customer");
		
		Product p1 = new Product();
		p1.setName("sss");
		p1.setSkuCode("TTYC43821K59160");
		
		Product p2 = new Product();
		p2.setName("bbb");
		p2.setSkuCode("TTYC43821K56165");
		
		info.getProducts().add(p1);
		info.getProducts().add(p2);
		

    XStream xStreamCDATA = XStreamFactory.buildXStreamCDATA();
    xStreamCDATA.alias("SyncProductInfo", SyncProductInfo.class);
    xStreamCDATA.alias("Product", Product.class);

    String xmlWithCDATA = xStreamCDATA.toXML(info);
        
		System.out.println(xmlWithCDATA);
	}

}

okhttp

写SDK是要往服务器上发请求,所以需要使用http库,简单起见使用okhttp,其实在android上很多应用也是用这个库的。

https://github.com/square/okhttp

OkHttp v2.0 depends on Okio v1.0. You can download its jar here. This worked for me

Reference from https://github.com/square/okhttp/issues/870

你还需要下载 Okio, OKhttp使用这个库用来快速的I/O处理 在这里下载 latest JAR.

get

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}

post

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

fastjson

阿里温少开发的最快的json解析库

用法就不用贴代码了,自己看 https://github.com/alibaba/fastjson

spring boot

  • github仓库 https://github.com/spring-projects/spring-boot
  • 中文资料 http://www.infoq.com/cn/articles/microframeworks1-spring-boot/

全文完

欢迎关注我的公众号【node全栈】

node全栈.png

转载于:https://my.oschina.net/nodeonly/blog/491541

你可能感兴趣的:(使用java开发SDK你可能会用的4个库)