FastJSON将LIST转JSON之后有反斜杠
@Service
@Slf4j
public class LiJingDeliverApi {
public RetStatus<Object> deliver(CustomerPlaformShopVO customerPlaformVO, OrderBondedInfoVO orderBondedInfoVO) {
RetStatus<Object> retStatus = new RetStatus<>();
try{
Map<String, Object> header = new HashMap<>();
header.put("app_key", customerPlaformVO.getF_app_id());
header.put("v", "1.0");
header.put("timestamp", LiJingUtils.getNowTime());
header.put("order_id", orderBondedInfoVO.getF_batch_no());
List<OrderGoods> orderGoods = new ArrayList<>();
for (OrderBondedGoodsVO orderBondedGoodsVO : orderBondedInfoVO.getOrderBondedGoodsVOS()) {
OrderGoods orderGood = new OrderGoods();
orderGood.setGoods_no(orderBondedGoodsVO.getF_goods_pre_no());
orderGood.setLogistics_name("快递名称");
orderGood.setLogistics_code("快递编码");
orderGood.setWay_bill_no(orderBondedInfoVO.getF_express_no());
orderGood.setDelivery_time(orderBondedInfoVO.getF_send_time());
orderGoods.add(orderGood);
}
header.put("order_goods", JSONArray.parseArray(JSONObject.toJSONString(orderGoods)));
header.put("sign", MD5Utils.getMD5(customerPlaformVO.getF_app_secret()
+ LiJingUtils.createLinkString(header)
+ customerPlaformVO.getF_app_secret()).toUpperCase());
String deliverOrder = LiJingUtils.sendPost(LiJingUtils.deliverUrl ,JSONObject.toJSONString(header));
DeliverOrder deliverInfo = JSONObject.parseObject(deliverOrder, DeliverOrder.class);
if (!StringUtils.equals(deliverInfo.getStatus(),"1")) {
retStatus.set(1, "订单号:" + deliverInfo.getOrder_id() + ",回传失败:" + deliverInfo.getNote());
return retStatus;
}
} catch (Exception e){
retStatus.set(1, "回传异常.");
e.printStackTrace();
}
return retStatus;
}
}
public class LiJingUtils {
public static final String deliverUrl="http://api.orders.logistics";
public static String getNowTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
public static String getSpecificTime(int day) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(nowDate);
cal.add(Calendar.DAY_OF_MONTH, day);
Date time = cal.getTime();
return df.format(time);
}
public static String createLinkString(Map<String, Object> params) {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
String prestr = "";
for(int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
String value = params.get(key).toString();
if (StringUtils.isBlank(value)) {
continue;
}
prestr = prestr + key + value;
}
return prestr;
}
public static String sendPost(String url, String body) {
String response = null;
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity stringentity = new StringEntity(body, ContentType.create("application/json", "UTF-8"));
httppost.setEntity(stringentity);
httpresponse = httpclient.execute(httppost);
response = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}
public static String createLinkString(Map<String, String> params) throws UnsupportedEncodingException {
List<String> keys = new ArrayList(params.keySet());
Collections.sort(keys);
String prestr = "";
for(int i = 0; i < keys.size(); ++i) {
String key = (String)keys.get(i);
String value = (String)params.get(key);
if(StringUtils.isBlank(value)){
continue;
}
if (i == keys.size() - 1) {
prestr = prestr + key + "=" + URLEncoder.encode(value,"utf-8");
} else {
prestr = prestr + key + "=" + URLEncoder.encode(value,"utf-8") + "&";
}
}
return prestr;
}
public static String sendPost(String url, Map<String, String> headers, String body) {
String response = null;
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity stringentity = new StringEntity(body, ContentType.create("application/json", "UTF-8"));
httppost.setEntity(stringentity);
Iterator headerIterator = headers.entrySet().iterator();
while (headerIterator.hasNext()) {
Entry<String, String> elem = (Entry<String, String>) headerIterator.next();
httppost.addHeader(elem.getKey(), elem.getValue());
}
httpresponse = httpclient.execute(httppost);
response = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}