java HttpClient调用wsdl协议
作者:互联网
以HttpClient访问wsdl协议,更加的灵活
import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.charset.Charset; /** * @ClassName EpWsdlUtil * @Description webservice调用工具类 * @Author kun * @Date 2021-11-12 23:38 * @Version 1.0 **/ @Component public class WsdlUtil { static Logger logger = LoggerFactory.getLogger(WsdlUtil.class); /** * HttpClient方式调用webservice api <br> * @Author kun * @Date * @Param [point, params, methodName] * @return java.lang.String */ private static String doPostWebServiceURL(String point, String params)throws Exception { String result = ""; // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(point); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); StringEntity data = new StringEntity(params, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { result = EntityUtils.toString(httpEntity, "UTF-8"); } } catch (Exception e) { logger.error("调用远程WebService接口异常:{}" , e); throw e; }finally { IOUtils.closeQuietly(closeableHttpClient); } return result; } /** * 根据实际情况封装SOAP数据进行访问 * @param methodName * @param token * @param object * @param url * @return * @throws Exception */ public static JSONObject obtainTokenByHttp(String methodName,String token,Object object , String url) throws Exception { String xmlData=XmlUtil.beanToXml(object,false); StringBuffer soapRequestParams = new StringBuffer(); soapRequestParams.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eop=\"http://epoint.com.cn\">"); soapRequestParams.append("<SOAP-ENV:Header/>"); soapRequestParams.append("<SOAP-ENV:Body>"); soapRequestParams.append(String.format("<eop:%s>",methodName) ); soapRequestParams.append(String.format("<Token>%s</Token>",token) ); soapRequestParams.append("<XML>"); soapRequestParams.append("<ROOT>"); soapRequestParams.append(xmlData); soapRequestParams.append("</ROOT>"); soapRequestParams.append("</XML>"); soapRequestParams.append(String.format("</eop:%s>",methodName) ); soapRequestParams.append("</SOAP-ENV:Body>"); soapRequestParams.append("</SOAP-ENV:Envelope>"); String returnDatabase =doPostWebServiceURL(url , soapRequestParams.toString()); returnDatabase=returnDatabase.replace("<","<").replace(">",">"); String head="<CaihongService>"; String tail="</CaihongService>"; String xmlResult=returnDatabase.substring(returnDatabase.indexOf(head)+head.length(),returnDatabase.indexOf("</CaihongService>")); com.alibaba.fastjson.JSONObject jsonObject =XmlUtil.xmlToJson(xmlResult); return jsonObject; } }WsdlUtil
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.*; import java.util.List; /** * @ClassName XmlUtil * @Description TODO * @Author kun * @Date 2021-11-13 0:06 * @Version 1.0 **/ public class XmlUtil { /** * xml转为JSONObject * @param xml * @return */ public static JSONObject xmlToJson(String xml) { return elementToJson((strToDocument(xml)).getRootElement()); } /** * json字符串转为xml * @param json * @return */ public static String jsonToXml(String json) { try { StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); JSONObject jObj = JSON.parseObject(json); String s= json2xml(jObj); buffer.append(s); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } } /** * 实体转为xml * @param object * @param hasVersion * @return */ public static String beanToXml(Object object,Boolean hasVersion){ try{ StringBuffer buffer=new StringBuffer(); JSONObject jObj= (JSONObject) JSONObject.toJSON(object); if(hasVersion){ buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); } String s= json2xml(jObj); buffer.append(s); return buffer.toString(); }catch (Exception e){ e.printStackTrace(); return ""; } } /** * xml字符串转为dom4j document * @param xml * @return */ private static Document strToDocument(String xml) { try { return DocumentHelper.parseText(xml); } catch (DocumentException e) { return null; } } /** * 递归xml节点对象转为jsonboject * @param node * @return */ private static JSONObject elementToJson(Element node) { JSONObject result = new JSONObject(); List<Attribute> listAttr = node.attributes(); for (Attribute attr : listAttr) { result.put(attr.getName(), attr.getValue()); } List<Element> listElement = node.elements(); if (!listElement.isEmpty()) { for (Element e : listElement) { if (e.attributes().isEmpty() && e.elements().isEmpty()) { result.put(e.getName(), e.getTextTrim()); } else { if (!result.containsKey(e.getName())) { result.put(e.getName(), new JSONArray()); } ((JSONArray) result.get(e.getName())).add(elementToJson(e)); } } } return result; } /** * JSONObject 转为xml字符串 * @param jsonObj * @return */ public static String json2xml(JSONObject jsonObj) { StringBuffer buff = new StringBuffer(); JSONObject tempObj = null; JSONArray tempArr = null; for (String temp : jsonObj.keySet()) { buff.append("<" + temp.trim().toUpperCase() + ">"); jsonObj.get(temp); if (jsonObj.get(temp) instanceof JSONObject) { tempObj = (JSONObject) jsonObj.get(temp); buff.append(json2xml(tempObj)); } else if (jsonObj.get(temp) instanceof JSONArray) { tempArr = (JSONArray) jsonObj.get(temp); if (tempArr.size() > 0) { for (int i = 0; i < tempArr.size(); i++) { tempObj = (JSONObject) tempArr.get(0); buff.append(json2xml(tempObj)); } } } else { String tempStr = jsonObj.get(temp).toString(); buff.append(tempStr.trim()); } buff.append("</" + temp.trim().toUpperCase() + ">"); } return buff.toString(); } }XmlUtil
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>porm.xml
@PostMapping("testService") @ApiOperation("测试") public JSONObject testService() throws Exception { Evaluat evaluat=new Evaluat(); evaluat.setPingBiaoIsEnd("1"); evaluat.setPingBiaoDate("20211112090900"); evaluat.setPlatFormCode("111"); evaluat.setProjectGuid("dddd"); evaluat.setRoomId("1111"); JSONObject jsonObject= WsdlUtil.obtainTokenByHttp("ReceivePingBiaoResult","123",evaluat,"http://xxxx/EpCaihongService/services/CaihongService"); return jsonObject; }测试
标签:java,String,JSONObject,soapRequestParams,import,return,wsdl,append,HttpClient 来源: https://www.cnblogs.com/kun2008/p/15547169.html