我写的纯SOAP WEBService
发布时间:2020-12-16 23:39:27 所属栏目:安全 来源:网络整理
导读:不多说,请看代码: package wll.webservcie.client.util;import java.beans.PropertyDescriptor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import javax.xml.soap.MessageFactory;import javax.x
不多说,请看代码: package wll.webservcie.client.util; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPConstants; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.hikvision.cms.webservcie.client.vo.BaseClientVO; import com.hikvision.cms.webservcie.client.vo.GetDirectorys; import com.hikvision.cms.webservcie.client.vo.GetTokenInfo; import com.hikvision.kms.filemgr.ws.webservice.domain.FileBean; import com.hikvision.swdf.exception.ProgramException; public class ClientUtil { /** Log */ private static final Logger log = LoggerFactory.getLogger(ClientUtil.class); /** * 整合wsdl */ private String wsdl; /** * wsdl的前缀 */ private String wsdlPrefix = "http://192.168.0.1:8080"; /** * method名 */ private String method; /** * 命名空间 */ private String nameSpace; /** * 创建 SOAP Connection * * @return * @throws SOAPException */ private static SOAPConnection getSoapConnection() throws SOAPException { // -- 使用SOAP连接工厂创建连接对象 final SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); final SOAPConnection soapConnection = soapConnectionFactory.createConnection(); return soapConnection; } private void initParams(@SuppressWarnings("rawtypes") Class clazz) throws IllegalArgumentException,IllegalAccessException { Field[] fields = clazz.getDeclaredFields(); for (Field aField : fields) { if ("nameSpace".equals(aField.getName())) { if (Modifier.isStatic(aField.getModifiers())) { aField.setAccessible(true); this.nameSpace = String.valueOf(aField.get(null)); } } if ("wsdl".equals(aField.getName())) { if (Modifier.isStatic(aField.getModifiers())) { aField.setAccessible(true); this.wsdl = this.wsdlPrefix + String.valueOf(aField.get(null)); } } if ("method".equals(aField.getName())) { if (Modifier.isStatic(aField.getModifiers())) { aField.setAccessible(true); this.method = String.valueOf(aField.get(null)); } } } } /** * 创建 SOAP Message * * @return * @throws SOAPException */ private SOAPMessage getSoapMessage() throws SOAPException { final MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); final SOAPMessage soapMessage = messageFactory.createMessage(); // -- 创建 SOAP 消息体 final SOAPPart soapPart = soapMessage.getSOAPPart(); final SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("web",this.nameSpace); return soapMessage; } public String execWs(BaseClientVO obj,@SuppressWarnings("rawtypes") Class clazz)throws ProgramException { try { // 初始化参数 initParams(clazz); // -- 创建SOAP传输对象 final SOAPMessage soapMessage = getSoapMessage(); // 设置消息头 fillHeader(soapMessage,obj); // 设置消息体 final SOAPBody soapBody = soapMessage.getSOAPBody(); final SOAPElement getMessage = soapBody.addChildElement(this.method,"web"); fillBody(soapMessage,getMessage,obj,clazz); soapMessage.saveChanges(); //TODO System.out.println("[soapBody]:"+soapBody); log.debug("[soapBody]:"+soapBody); return getResult(soapMessage); } catch (Exception e) { log.error("WsClientUtil::execWs::访问WebService发生异常:" + e); e.printStackTrace(); throw new ProgramException("WsClientUtil.execWs.Exception",e); } } private String getResult(SOAPMessage soapMessage) throws SOAPException { // -- 连接服务并获得返回的状态码 final SOAPConnection soapConnection = getSoapConnection(); final SOAPMessage soapMessageReply = soapConnection.call(soapMessage,this.wsdl); org.w3c.dom.Node node = soapMessageReply.getSOAPBody().getFirstChild().getFirstChild(); final String statusCode = node.toString(); soapConnection.close(); return statusCode; } /** * @param soapMessage * @param obj * @throws SOAPException */ private void fillHeader(SOAPMessage soapMessage,BaseClientVO obj) throws SOAPException{ if(obj instanceof GetTokenInfo){ soapMessage.getSOAPHeader().detachNode(); return ; } SOAPHeader soapHeader = soapMessage.getSOAPHeader(); SOAPElement requsetHeader = soapHeader.addChildElement("RequestSOAPHeader","tns","http://ws.cms.hikvision.com/"); SOAPElement token = requsetHeader.addChildElement("token","tns"); token.setAttribute("xmlns","http://ws.cms.hikvision.com/"); token.addTextNode(obj.getToken()); log.debug("[soapHeader]:"+soapHeader); System.out.println("[soapHeader]:"+soapHeader); } private void fillBody(final SOAPMessage soapMessage,SOAPElement getMessage,BaseClientVO obj,Class clazz) throws Exception { // -- 添加参数节点 Field[] fields = clazz.getDeclaredFields(); for (Field aField : fields) { log.debug("变量名:" + aField.getName()); log.debug("变量类型:" + aField.getType().getSimpleName()); if (Modifier.isStatic(aField.getModifiers()) == false) { PropertyDescriptor pd = new PropertyDescriptor(aField.getName(),clazz); Method getMethod = pd.getReadMethod();// 获得get方法 Object aValue = getMethod.invoke(obj);// 执行get方法返回一个Object if("file".equals(aField.getName())){ FileBean fileBean = (FileBean) aValue; SOAPElement file = getMessage.addChildElement(aField.getName()); file.addChildElement("fileName").addTextNode(fileBean.getFileName()); soapMessage.addAttachmentPart(soapMessage.createAttachmentPart(fileBean.getDataHandler())); //file.addChildElement("dataHandler").addTextNode(String.valueOf(fileBean.getDataHandler())); //file.addChildElement("dataHandler").addTextNode(fileBean.getDataHandler().get); } // 如果该对象的值不为空,则赋值 if (aValue != null) { getMessage.addChildElement(aField.getName()).addTextNode(String.valueOf(aValue)) .setAttribute("type","xs:" + aField.getType().getSimpleName()); }else{ getMessage.addChildElement(aField.getName()).addTextNode(""); } } } } public String getWsdl() { return wsdl; } public void setWsdl(String wsdl) { this.wsdl = wsdl; } public String getWsdlPrefix() { return wsdlPrefix; } public void setWsdlPrefix(String wsdlPrefix) { this.wsdlPrefix = wsdlPrefix; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getNameSpace() { return nameSpace; } public void setNameSpace(String nameSpace) { this.nameSpace = nameSpace; } /** * 测试主函数:807d6bf90000014549ba59ab96d9429a * * @param args */ public static void main(String[] args) { ClientUtil webServiceSample = new ClientUtil(); try { GetDirectorys g = new GetDirectorys(); g.setToken(GetTokenInfo.getStaticToken("administrator","123456")); String result = webServiceSample.execWs(g,g.getClass()); System.out.println("result:" + result); } catch (Exception e) { } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- angular – 使用mat-menu作为上下文菜单:正确定位
- Domino/Xpages Bootstrap 动态生成首页功能
- Bootstrap 学习之 (十七)------ 面板
- angularjs – 没有使用Angular $http设置Content-Type标头
- typescript – 如何在Angular2/4/5中实现自定义异步验证器
- vim练习
- Bootstrap3.0入门学习系列教程
- twitter-bootstrap – twitter bootstrap支持的转换的类名是
- Redis简单案例(一) 网站搜索的热搜词
- bootstrap fileinput 组件整合SpringMVC上传图片到本地磁盘