顺丰下单后处理接收到的xml
生活随笔
收集整理的這篇文章主要介紹了
顺丰下单后处理接收到的xml
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
xml字符串轉Java對象
開始接觸順豐下單,下單完成后返回的居然是xml,早聽說數據傳送的兩種格式json和xml。首先看到xml一臉迷茫,不知道從哪下手,最后查閱了資料,成功的將xml字符串轉換成Java對象,不說那么多了,直接上代碼:
import java.io.Serializable; import java.util.List;import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlValue;import lombok.Data;/*** 順豐接口response對象*/@XmlAccessorType(XmlAccessType.FIELD)@XmlRootElement(name = "Response") @Data public class SfExpressResponse implements Serializable {private static final long serialVersionUID = 1L;//響應狀態@XmlElement(name = "Head")private String Head;//響應失敗原因@XmlElement(name = "ERROR")private ERROR ERROR;//響應結果@XmlElement(name = "Body")private Body Body;@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class ERROR {@XmlAttribute(name = "code")private String code;@XmlValueprivate String text;}@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class Body {@XmlElement(name = "OrderResponse")private OrderResponse OrderResponse;@XmlElement(name = "RouteResponse")private RouteResponse RouteResponse;}@XmlRootElement(name="OrderResponse")@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class OrderResponse {//訂單號@XmlAttribute(name = "orderid")private String orderId;//運單號@XmlAttribute(name = "mailno")private String mailNo;//原寄地區域代碼(可用于順豐電子運單標簽打印)@XmlAttribute(name = "origincode")private String originCode;//目的地區域代碼(可用于順豐電子運單標簽打印)@XmlAttribute(name = "destcode")private String destCode;//篩單結果:1:人工確認 2:可收派 3:不可以收派@XmlAttribute(name = "filter_result")private String filterResult;@XmlElement(name = "rls_info")private Rls_info rls_info;}@XmlRootElement(name="rls_info")@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class Rls_info {@XmlAttribute(name = "invoke_result")private String invoke_result;@XmlElement(name = "rls_detail")private Rls_detail rls_detail;}@XmlRootElement(name="rls_detail")@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class Rls_detail {@XmlAttribute(name = "sourceTransferCode")private String sourceTransferCode;@XmlAttribute(name = "sourceCityCode")private String sourceCityCode;@XmlAttribute(name = "sourceDeptCode")private String sourceDeptCode;@XmlAttribute(name = "sourceTeamCode")private String sourceTeamCode;@XmlAttribute(name = "destCityCode")private String destCityCode;@XmlAttribute(name = "destDeptCode")private String destDeptCode;@XmlAttribute(name = "destRouteLabel")private String destRouteLabel;@XmlAttribute(name = "cargoTypeCode")private String cargoTypeCode;@XmlAttribute(name = "limitTypeCode")private String limitTypeCode;@XmlAttribute(name = "expressTypeCode")private String expressTypeCode;@XmlAttribute(name = "twoDimensionCode")private String twoDimensionCode;@XmlAttribute(name = "proCode")private String proCode;}@XmlRootElement(name="RouteResponse")@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class RouteResponse {//運單號@XmlAttribute(name = "mailno")private String mailNo;//路由@XmlElement(name = "Route")private List<Route> Route ;}@XmlRootElement(name="Route")@XmlAccessorType(XmlAccessType.NONE)@Datapublic static class Route {//路由節點發生的時間@XmlAttribute(name = "accept_time")private String acceptTime;//路由節點具體描述@XmlAttribute(name = "remark")private String remark;//路由節點操作碼@XmlAttribute(name = "opcode")private String opcode;} }上述,是完整的接收下單返回的xml字符串的值,若有缺失,可以仿照上邊的屬性進行補充。
在請求順豐下單時,需要將數據轉換成xml格式,我用的是在網上找的,如有遺漏請見諒:
工具類:
獲取數據生成xml請求字符串:
import java.util.Map;import org.springframework.beans.factory.annotation.Value;import com.common.util.StringUtil;import lombok.Data;@Data//這個是引入的jar中的注解相當于給屬性添加get/set方法 public class ShunFengUtil {@Value("${sf.clientCode}")private String clientCode; //商戶號 (短的字母拼接的字符串)custid 是一串數字@Value("${sf.checkword}")private String checkword; //驗證碼 (這個是一長串字符)@Value("${sf.custid}")private String custid=""; //月付id 我這里使用的是寄付月結/*** 順豐下單接口* @param params* @return*/public static String getOrderServiceRequestXml(Map<String, String> params) {StringBuilder strBuilder = new StringBuilder();strBuilder.append("<Request service='OrderService' lang='zh-CN'>");strBuilder.append("<Head>" + StringUtil.getSystemPropertie("clientCode") + "</Head>");strBuilder.append("<Body>");strBuilder.append("<Order").append(" ");strBuilder.append("orderid='" + params.get("orderid") + "'").append(" ");//返回順豐運單號strBuilder.append("express_type='1'").append(" ");//寄件方信息strBuilder.append("j_province='河南省'").append(" ");strBuilder.append("j_city='鄭州市'").append(" ");strBuilder.append("j_county='高新區'").append(" ");strBuilder.append("j_company='****業'").append(" ");strBuilder.append("j_contact='張三'").append(" ");strBuilder.append("j_tel='158******85'").append(" ");strBuilder.append("j_address='常春路'").append(" ");//收件方信息strBuilder.append("d_province='" + params.get("d_province") + "'").append(" ");strBuilder.append("d_city='" + params.get("d_city") + "'").append(" ");strBuilder.append("d_county='" + params.get("d_county") + "'").append(" ");strBuilder.append("d_company='" + params.get("d_company") + "'").append(" ");strBuilder.append("d_tel='" + params.get("d_tel") + "'").append(" ");strBuilder.append("d_contact='" + params.get("d_contact") + "'").append(" ");strBuilder.append("d_address='" + params.get("d_address") + "'").append(" ");//貨物信息strBuilder.append("express_type='1'").append(" ");strBuilder.append("pay_method='1'").append(" ");strBuilder.append("custid ='" + StringUtil.getSystemPropertie("custid") + "'").append(" ");strBuilder.append("parcel_quantity='1'").append(" ");strBuilder.append("is_docall='0'").append(" ");strBuilder.append("sendstarttime=''").append(" ");strBuilder.append("order_source='藥品'").append(">");strBuilder.append("<AddedService name='COD' value='1.01' value1='3712662207' />");strBuilder.append("</Order>");strBuilder.append("</Body>");strBuilder.append("</Request>");return strBuilder.toString();}/*** 獲取順豐訂單結果查詢接口xml* @param params* @return*/private String getOrderSearchServiceRequestXml(Map<String,String> params){String orderNo = params.get("orderNo");StringBuilder strBuilder = new StringBuilder();strBuilder.append("<Request service='OrderSearchService' lang='zh-CN'>");strBuilder.append("<Head>" + clientCode + "</Head>");strBuilder.append("<Body>");strBuilder.append("<OrderSearch").append(" ");strBuilder.append("orderid='" + orderNo.toString().trim() + "" + "'").append(" > ");strBuilder.append("</OrderSearch>");strBuilder.append("</Body>");strBuilder.append("</Request>");return strBuilder.toString();}/*** 獲取順豐物流查詢接口xml** @param params* @return*/public static String getRouteServiceRequestXml(Map<String, String> params) {StringBuilder strBuilder = new StringBuilder();strBuilder.append("<Request service='RouteService' lang='zh-CN'>");strBuilder.append("<Head>" + StringUtil.getSystemPropertie("clientCode") + "</Head>");strBuilder.append("<Body>");strBuilder.append("<RouteRequest").append(" ");strBuilder.append("tracking_type='1'").append(" ");strBuilder.append("method_type='1'").append(" ");strBuilder.append("tracking_number='" + params.get("mailno") + "'").append(" >");strBuilder.append("</RouteRequest>");strBuilder.append("</Body>");strBuilder.append("</Request>");return strBuilder.toString();}/*** 取消訂單** @param params* @return*/public static String getConfirmRequestXml(Map<String, String> params) {StringBuilder strBuilder = new StringBuilder();strBuilder.append("<Request service='OrderConfirmService' lang='zh-CN'>");strBuilder.append("<Head>").append(params.get("clientCode")).append("</Head>");strBuilder.append("<Body>");strBuilder.append("<OrderConfirm").append(" ");strBuilder.append("orderid='").append("orderNo").append("' ");strBuilder.append("dealtype='2'>").append(" ");strBuilder.append("</OrderConfirm>");strBuilder.append("</Body>");strBuilder.append("</Request>");return strBuilder.toString();}}順豐的電子面單打印的話,官網上就有Java的demo
總結
以上是生活随笔為你收集整理的顺丰下单后处理接收到的xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱视直播网络电视 v07.0201.01
- 下一篇: 根据数据使用arcGIS画分级图