注明:本文的环境是在SSM框架下进行的。文中的地址是随便写的,仅做举例使用
(1)application.properties文件
application.DWWebService = http://180.230.40.31:6080/CRMInterface.asmx
(2)编写ApplicationConfig类
@Configuration
@PropertySource(value="classpath:application.properties",encoding="utf-8")
public class ApplicationConfig {
@Value("${application.DWWebService}")
private String applicationDWWebService;
public String getApplicationDWWebService() {
return applicationDWWebService;
}
public void setApplicationDWWebService(String applicationDWWebService) {
this.applicationDWWebService = applicationDWWebService;
}
}
(3)编写WebServiceUtil类
@Component
public class WebServiceUtil {
private static ApplicationConfig applicationConfig;
@Autowired
public WebServiceUtil(ApplicationConfig applicationConfig) {
WebServiceUtil.applicationConfig = applicationConfig;
}
/**
* 请求头生成
* @return
*/
private static String getSoapHeader(){
StringBuffer soapHeader = new StringBuffer();
soapHeader.append("<soapenv:Header>");
soapHeader.append("<q0:Header>");
soapHeader.append("<q0:UserName>test</q0:UserName>");
soapHeader.append("<q0:PassWord>test1234</q0:PassWord>");
soapHeader.append("</q0:Header>");
soapHeader.append("</soapenv:Header>");
return soapHeader.toString();
}
//调用对方接口的XML
public static String createCustomerSyncXml(AjaxJson json) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", json.getCode());
jsonObject.put("message", json.getMessage());
jsonObject.put("attributes", json.getAttributes());
StringBuffer xml = new StringBuffer();
xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://tempuri.org/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
xml.append(getSoapHeader());
xml.append("<soapenv:Body>");
xml.append("<q0:TSOrderWeb_CustomerSync>");
xml.append("<q0:CustomerJson>");
xml.append(jsonObject);
xml.append("</q0:CustomerJson>");
xml.append("</q0:TSOrderWeb_CustomerSync>");
xml.append("</soapenv:Body>");
xml.append("</soapenv:Envelope>");
return xml.toString();
}
//解析XML,获取调用接口的返回值
public static Map<String, String> syncCrmDealerToM3(AjaxJson json) {
Map<String, String> resultMap = new HashMap<String, String>();
resultMap.put("result", "");
resultMap.put("errorMsg", "");
String xml = createCustomerSyncXml(json);
Document document = WebServiceUtil.parse2Document(
WebServiceUtil.sendXML(applicationConfig.getApplicationDWWebService(), xml));
if (document != null) {
Element rootElement = document.getRootElement();
if (rootElement != null) {
Element body = rootElement.element("Body");
if (body != null) {
Element responseElement = body.element("TSOrderWeb_CustomerSyncResponse");
if (responseElement != null) {
Element resultElement = responseElement.element("result");
Element errorMsgElement = responseElement.element("errorMsg");
if (resultElement != null) {
resultMap.put("result", resultElement.getTextTrim());
}
if (errorMsgElement != null) {
resultMap.put("errorMsg", errorMsgElement.getTextTrim());
}
}
}
}
}
return resultMap;
}
/**
* 生成解析文档
*
* @param xmlStr
* 解析的XML对象
* @return 返回一个document对象
*
*/
public static Document parse2Document(String xmlStr) {
Document document = null;
try {
document = DocumentHelper.parseText(xmlStr);
} catch (DocumentException e) {
log.error("读取classpath下xmlFileName文件发生异常,请检查CLASSPATH和文件名是否存在!", e);
}
return document;
}
/**
*
* 调用接口,发送xml文档获取数据
*
* @param url
* 请求url
* @param soapAction
* 请求action
* @param xml
* XML请求文档
* @return 用车接口系统提供的XML数据
*/
public static String sendXML(String url, String xml) {
HttpURLConnection conn = null;
InputStream in = null;
InputStreamReader isr = null;
OutputStream out = null;
StringBuffer result = null;
try {
byte[] sendbyte = xml.getBytes("UTF-8");
URL connUrl = new URL(url);// 连接RUL
conn = (HttpURLConnection) connUrl.openConnection();
conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
conn.setRequestProperty("Content-Length", sendbyte.length + "");// 设置文件长度
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(1000 * 30);// 设置连接超时30000ms
conn.setReadTimeout(60000);// 设置读取数据超时
out = conn.getOutputStream();
out.write(sendbyte);
if (conn.getResponseCode() == 200) {
result = new StringBuffer();
in = conn.getInputStream();
isr = new InputStreamReader(in, "UTF-8");
char[] c = new char[1024];
int a = isr.read(c);
while (a != -1) {
result.append(new String(c, 0, a));
a = isr.read(c);
}
}
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
return "";
} catch (IOException e) {
log.error(e.getMessage(), e);
return "";
} finally {
if (conn != null) {
conn.disconnect();
}
try {
if (in != null) {
in.close();
}
if (isr != null) {
isr.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result == null ? "" : result.toString();
}
}
(4)在controller层的方法中调用syncCrmDealerToM3()方法,传入相应的参数即可