自学内容网 自学内容网

JAVA 接收XML转实体类

一、接收xml
@PostMapping(value = “/refund/callback”, consumes = “text/xml”, produces = “text/xml”)
@Operation(summary = “回调接口”)
public String refundCallback(@RequestBody LeShuaRefundCallbackXml leShuaPayCallbackXml) {
try {
log.info(“LeShua Pay Callback RequestBody {}”, JSONObject.toJSONString(leShuaPayCallbackXml));
// 业务逻辑
} catch (Exception e) {
log.error(“LeShua Pay Callback Param {}”, JSONObject.toJSONString(leShuaPayCallbackXml));
log.error("LeShua PayCallbackController.payCallback Failed. ", e);
return “FAILED”;
}
}

二、请求返回xml格式转换
// 统一支付
Map<String, Object> map = new HashMap<>(ConstantNumeral.EIGHT.value());
map.put(“service”, “query_status”);
map.put(“sign_type”, “MD5”);
map.put(“merchant_id”,merchantNo);
map.put(“third_order_id”, payId);
map.put(“nonce_str”, System.currentTimeMillis());
String sign = PockytUtil.generateSortString(map, false) + “&key=” + key;
String signMd5 = SecureUtil.md5(sign).toUpperCase();
map.put(“sign”, signMd5);
HttpRequest post = HttpUtil.createPost(“https://paygate.leshuazf.com/cgi-bin/lepos_pay_gateway.cgi”);
post.header(“Content-Type”, “application/x-www-form-urlencoded”);
post.form(BeanUtil.beanToMap(map));
try (HttpResponse httpResponse = post.execute()) {
String response = httpResponse.body();
log.info(“handleLeShua Pay response: {}”, response);
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(response);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(jsonNode);
log.info(“handleLeShua Pay json: {}”, json);
return JSONObject.parseObject(json, LeShuaPayCallbackXml.class);
} catch (Exception e) {
log.error(“handleLeShua Pay error: {}”, e.getMessage());
}


原文地址:https://blog.csdn.net/qq_41867674/article/details/142454439

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!