使用java開發SDK你可能會用的4個庫

輸入圖片說明

使用java開發SDK你可能會用的4個庫java

  • xstream
  • okhttp
  • fastjson
  • spring boot

xstream

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

用法特別簡單android

定義javabeangit

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;
	}
}

而後寫個簡單的測試github

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);
	}
}

返回結果spring

//		<?xml version=」1.0」 encoding=」UTF-8」?>
//		<GetAsnStatus>
//		<customerCode>TB00002</customerCode>
//			<warehouseCode>BEST-BJ-001</warehouseCode>
//			<asnCode>ASN200909100033445</asnCode>
//		</GetAsnStatus>

xstream with cdata

先實現一個Writer

CustomWriterjson

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("<![CDATA[");
            writer.write(text);
            writer.write("]]>");
        } else {
            writer.write(text);
        }
    }
}

說明一下,這裏startNode方法是給全部的String類型的value增長cdata,若是有本身的業務,能夠在此處判斷。服務器

本身實現一個Xpp驅動,返回上面自定的writer

CustomXppDriverapp

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,邏輯就很清晰了dom

測試一下

注意:依賴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

全文完

歡迎關注個人公衆號【node全棧】

node全棧.png

相關文章
相關標籤/搜索