上一篇讲到我们用的是xml与bean之间的转换的方式来处理数据,那么就需要用到一些类库来解决,网上搜一下JAXBContext,就有很多相关的文章介绍(感谢大咖们的贡献),主要涉及两个方法如下:
/**
* java对象转换为xml文件
* @param obj 对象数据
* @param load java对象.Class
* @return xml文件的String
* @throws JAXBException
*/
public static String beanToXml(Object obj,Class<?> load) throws JAXBException{
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); //去掉头部
StringWriter writer = new StringWriter();
marshaller.marshal(obj,writer);
return writer.toString();
}
/**
* xml文件配置转换为对象
* @param xml xml数据
* @param load java对象.Class
* @return java对象
* @throws JAXBException
* @throws IOException
*/
public static Object xmlToBean(String xml, Class<?> load) throws JAXBException, IOException{
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object object = unmarshaller.unmarshal(new StringReader(xml));
return object;
}
另外与丰桥的数据对接,必然要用到http工具类,可以使用HttpClient,或者Okhttp等,看个人喜欢,参考如下:
/**
* @param url
* @param content
* @return
* @throws Exception
*/
public static String post(String url, Map params) throws Exception {
HttpClient httpclient = getHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
NameValuePair[] nvps = new NameValuePair[params.size()];
int index = 0;
for(java.util.Iterator it = params.keySet().iterator(); it.hasNext(); ) {
String key = (String) it.next();
nvps[index++] = new BasicNameValuePair(key, (String) params.get(key));
}
httppost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
HttpResponse response = null;
response = httpclient.execute(httppost);
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
return (resEntity ==null) ?null : EntityUtils.toString(resEntity);
}
有了上面这个方法,就可以开始测试接口是否正常了,如下:
private static void testRequest() throws Exception {
String reqURL="http://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService";
String reqXml="<Request service = \"OrderService\" lang = \"zh-CN\" > \n" +
"<Head>SLKJ2019</Head>\n" +
"<Body>\n" +
" <Order \n" +
" orderid=\"SFKD-20160219000019\" \n" +
" j_company=\"深圳宝龙达信息技术股份有限公司\" \n" +
" j_contact=\"邓丽君\" \n" +
" j_tel=\"15323233432\" \n" +
" j_mobile=\"15322234342\" \n" +
" j_province=\"广东省\" \n" +
" j_city=\"深圳市\"\n" +
" j_county=\"南山区\"\n" +
" j_address=\"广东省深圳市南山区西丽镇塘朗同富裕工业城7栋\" \n" +
" d_contact=\"四海\" d_tel=\"15023434543\" \n" +
" d_mobile=\"15423456545\" \n" +
" d_province=\"广东省\" \n" +
" d_city=\"深圳市\" \n" +
" d_county=\"南山区\" \n" +
" d_address=\"科技园软件产业基地\" \n" +
" express_type=\"1\" \n" +
" pay_method=\"1\" \n" +
" custid=\"7551234567\" \n" +
" parcel_quantity=\"1\" \n" +
" is_docall=\"0\" \n" +
" sendstarttime=\"\" \n" +
" remark=\"电子产品 笔记本+显卡\"\n" +
" is_unified_waybill_no=\"1\">\n" +
"</Order>\n" +
"</Body> \n" +
"</Request>";//接口请求对应得xml报文
String myReqXML = reqXml.replaceAll("SLKJ2019", clientCode);
String verifyCode = md5EncryptAndBase64(myReqXML + checkWord);
Map map = new LinkedHashMap();
map.put("xml", myReqXML);
map.put("verifyCode", verifyCode);
String response = HttpClientUtils.post(reqURL, map);;
System.out.println(response);
}
custid,clientCode,checkWord要换成自己的,没什么意外的话,返回结果类似官方给出的:
<Response service="OrderService">
<Head>OK</Head>
<Body>
<OrderResponse filter_result="2" destcode="020" mailno="SF4448355589333" origincode="755" orderid="QIAO-20180524001"/>
</Body>
</Response>
好了,先整杯好茶再继续……
网友评论