|
|
@@ -0,0 +1,103 @@
|
|
|
+package com.jzg.yongcheng.utils;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.*;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
|
+import com.jzg.commons.exception.SystemException;
|
|
|
+import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.reactive.function.client.WebClient;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class RequestApiComponents {
|
|
|
+
|
|
|
+ private WebClient webClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WebClient.Builder webClientBuilder;
|
|
|
+
|
|
|
+ private ObjectMapper gainXmlMapper() {
|
|
|
+ ObjectMapper xmlMapper = new XmlMapper();
|
|
|
+ xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+ xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
|
|
|
+ xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
|
|
|
+ xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
|
|
|
+ xmlMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
|
|
+ return xmlMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ public <Q, S> Mono<S> post(Q q, Class<S> tClass) throws JsonProcessingException {
|
|
|
+ String url = "http://39.98.214.159/yongcheng/NewQueryYwCvrgWebService";
|
|
|
+ // 设置请求头
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ httpHeaders.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
|
|
|
+ httpHeaders.setContentType(MediaType.parseMediaType("text/xml;charset=UTF-8"));
|
|
|
+ webClient = webClientBuilder.baseUrl(url)
|
|
|
+ .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_XML_VALUE)
|
|
|
+ .defaultHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 构造 SOAP XML 请求体
|
|
|
+ String xmlString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"http://newWS.front" +
|
|
|
+ ".app.echannel.ebiz.alltrust.com/\">";
|
|
|
+ xmlString += "<soapenv:Body>";
|
|
|
+ xmlString += gainXmlMapper().writeValueAsString(q); // 将请求对象转换为 XML 字符串
|
|
|
+ xmlString += "</soapenv:Body>\n" +
|
|
|
+ "</soapenv:Envelope>";
|
|
|
+
|
|
|
+ // 使用 WebClient 发起请求
|
|
|
+ return webClient
|
|
|
+ .post()
|
|
|
+ .bodyValue(xmlString) // 设置请求体
|
|
|
+ .retrieve() // 发起请求
|
|
|
+ .bodyToMono(String.class) // 将响应转换为 Mono<String>
|
|
|
+ .map(responseBody -> {
|
|
|
+ // 处理响应
|
|
|
+ String responseStringMsg = StringEscapeUtils.unescapeXml(responseBody);
|
|
|
+ String cleanStr = cleanResponseStr(responseStringMsg);
|
|
|
+
|
|
|
+ if (!cleanStr.contains("<")) {
|
|
|
+ return (S) cleanStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将响应的 XML 字符串反序列化成目标对象
|
|
|
+ try {
|
|
|
+ S bean = gainXmlMapper().readValue(cleanStr, tClass);
|
|
|
+ if (bean == null) {
|
|
|
+ throw new SystemException("请求失败");
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new SystemException("解析响应失败", e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String cleanResponseStr(String responseData) {
|
|
|
+ String rStr = "";
|
|
|
+ String str = "";
|
|
|
+ if (responseData.indexOf("<response>") > 0) {
|
|
|
+ rStr = responseData.substring(responseData.indexOf("<response>") + 10, responseData.indexOf("</response>"));
|
|
|
+ } else if (responseData.indexOf("<return>") > 0) {
|
|
|
+ rStr = responseData.substring(responseData.indexOf("<return>") + 8, responseData.indexOf("</return>"));
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(rStr)) {
|
|
|
+ rStr = responseData.substring(responseData.indexOf("<response>") + 10, responseData.indexOf("</response>"));
|
|
|
+ }
|
|
|
+ String lRStr = rStr.replaceAll("<", "<");
|
|
|
+ str = lRStr.replaceAll(">", ">");
|
|
|
+
|
|
|
+ return str.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|