• WebService 接口的发布调试与调用


    背景介绍:内网,调用他方服务接口;基于WebService引擎Axis2.

    老项目环境:tomcat 8.5 + Servlet + JSP + Mysql

    注:Springboot环境下的Axis2 集成及其WebService 注解方式集成 另附,可自行百度。

    (一)利用postman调试对外提供的WebService接口

    此处以调试以本地接口为例:

    https://192.168.4.110:8080/**/services/MSAccountManagerService?wsdl

     然后再对postman进行以下设置:

    (1)复制连接,自动添加 Params项

    (2)重写Headers项的Content-Type为text/xml

    此处若不设置,将会出错:

    <html><body><h2>Please enable REST support in WEB-INF/conf/axis2.xml and WEB-INF/web.xml</h2></body></html>

     (3)构建参数

    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <soapevn:Envelope xmlns:soapevn="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://pojo.axis.**.com">
    3. <soapevn:Body>
    4. <test:delUser xmlns="http://www.w3.org/2001/XMLSchema">
    5. <userIDs><![CDATA[<accounts><accId>lei000</accId><accId>lei111</accId></accounts>]]></userIDs>
    6. <test:delUser>
    7. </soapevn:Body>
    8. </soapenv:Envelope>

    (4)由于是https 可进行 SSL 绕过认证:

    (二)调用服务

    此处以调用以下面接口为例:

    http://10.102.102.136:9080/JKService/webservices/BussinessSupportService?wsdl

     查看提供的接口地址是否可用:

    wget -o x.wsdl http://10.102.102.136:9080/JKService/webservices/BussinessSupportService?wsdl

    使用eclipse自带的WebService生成工具,根据以上客户提供的WebService接口,生成对应代码:

    (1)右键点击项目名称——>“NEW”——>“Other”——>输入web service client 如下:

    (2)因为是内网环境,无法输入上述接口地址直接生成,因此:

     将客户提供的静态 BussinessSupportService.xml 直接更改文件后缀名为.wsdl,然后再Browser

    更改好的.wsdl文件

     即可生成以下文件:

     注意:axis2的高版本jar生成的文件内容可能会有所不同,比如多实现了个别方法。

    (3)生成后就如同调用本地方法一般:

    1. /**
    2. * 调用第三方webservice接口
    3. *
    4. * @throws AxisFault
    5. * @throws ServiceException
    6. */
    7. private static String todoWebService(String methodName, String paramXml) throws AxisFault, ServiceException {
    8. BussinessSupportServicePortTypeProxy proxy = new BussinessSupportServicePortTypeProxy();
    9. proxy.setEndpoint(URL);
    10. BussinessSupportServicePortType wrapper = proxy.getBussinessSupportServicePortType();
    11. String result = "";
    12. try {
    13. if ("queryAppOperJKStatus".equals(methodName)) {
    14. result = wrapper.queryAppOperJKStatus(paramXml);
    15. } else if ("createAppRequest".equals(methodName)) {
    16. result = wrapper.createAppRequest(paramXml);
    17. } else if ("remoteAuth".equals(methodName)) {
    18. result = wrapper.remoteAuth(paramXml);
    19. } else if ("reSendJKPass".equals(methodName)) {
    20. result = wrapper.reSendJKPass(paramXml);
    21. } else if ("queryJKStatusByID".equals(methodName)) {
    22. result = wrapper.queryJKStatusByID(paramXml);
    23. }
    24. } catch (RemoteException e) {
    25. e.printStackTrace();
    26. }
    27. return result;
    28. }

     (4)获取到的结果为字符串:结合业务利用AXIOMUtil.stringToOM(result) 解析即可。

    1. import org.apache.axiom.om.util.AXIOMUtil;
    2. /**
    3. * 将接口返回结果解析成实体
    4. *
    5. * @param result
    6. * @return
    7. * @throws Exception
    8. */
    9. @SuppressWarnings("rawtypes")
    10. private static JkResendPassResult parseResendPassResult(String result) {
    11. // 结果数据
    12. JkResendPassResult jkresult = new JkResendPassResult();
    13. OMElement accElement = null;
    14. try {
    15. accElement = AXIOMUtil.stringToOM(result);
    16. } catch (XMLStreamException e) {
    17. logger.error(e.getCause().toString());
    18. return jkresult;
    19. }
    20. logger.info(accElement.toString());
    21. // Iterator childElements = accElement.getChildElements();
    22. Iterator children = accElement.getChildren();
    23. while (children.hasNext()) {
    24. OMElement info = (OMElement) children.next();
    25. logger.info(info.toString());
    26. String localName = info.getLocalName();
    27. String text = info.getText().trim();
    28. if ("resultCode".equals(localName)) {
    29. jkresult.setResultCode(text);
    30. } else if ("requestID".equals(localName)) {
    31. jkresult.setRequestID(text);
    32. } else if ("resendResult".equals(localName)) {
    33. jkresult.setResendResult(text);
    34. }
    35. }
    36. return jkresult;
    37. }

     (5)调用接口前构建xml参数,自己简单封装的适用于本业务场景一个小工具类:主要是因为项目中的axis2 版本比较低,市面官方jar包中封装的方法无法适用,在次记录而已,可自行百度。

    1. import java.lang.reflect.Field;
    2. import java.lang.reflect.Method;
    3. import java.text.DecimalFormat;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. import java.util.List;
    7. import javax.xml.bind.annotation.XmlElement;
    8. import org.apache.axiom.om.OMAbstractFactory;
    9. import org.apache.axiom.om.OMElement;
    10. import org.apache.axiom.om.OMFactory;
    11. import org.apache.axiom.om.OMNamespace;
    12. import org.apache.commons.lang.StringUtils;
    13. /**
    14. * 将实体类转换成xml格式(OMElement)
    15. *
    16. * @author shilei
    17. * @date 2017-08-16
    18. *
    19. */
    20. public class OMElementUtils {
    21. public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    22. public static String convertListToXml(List<Object> list, String objRootName, String listRootName) throws Exception {
    23. StringBuffer result = new StringBuffer();
    24. result.append("<").append(listRootName).append(">");
    25. for (Object object : list) {
    26. String subXmlStr = convertSingle(object, objRootName);
    27. result.append(subXmlStr);
    28. }
    29. result.append("</").append(listRootName).append(">");
    30. return result.toString();
    31. }
    32. public static String convertSingle(Object t, String rootName) throws Exception {
    33. OMFactory fac = OMAbstractFactory.getOMFactory();
    34. OMNamespace ns1 = fac.createOMNamespace("", "");
    35. String root = rootName;
    36. OMElement rootEle = fac.createOMElement(root, ns1);
    37. Field[] fields = t.getClass().getDeclaredFields();
    38. for (Field field : fields) {
    39. XmlElement xmlElement = field.getAnnotation(XmlElement.class);
    40. if (xmlElement == null) {
    41. continue;
    42. }
    43. String eleName = xmlElement.name();
    44. if (StringUtils.isEmpty(eleName) || "##default".equals(eleName)) {
    45. eleName = field.getName();
    46. }
    47. OMElement eleId = fac.createOMElement(eleName, ns1, rootEle);
    48. String fieldValue = getFieldValue(t, eleName);
    49. if (StringUtils.isNotBlank(fieldValue))
    50. eleId.setText(fieldValue);
    51. else
    52. eleId.setText("**");
    53. }
    54. String paramStr = rootEle.toString();
    55. paramStr = paramStr.replace("**", "");
    56. return paramStr;
    57. }
    58. public static <T> String getFieldValue(T t, String fieldName) throws Exception {
    59. Method method = t.getClass().getMethod("get" + StringUtils.capitalize(fieldName));
    60. if (method == null) {
    61. return null;
    62. }
    63. Object obj = method.invoke(t);
    64. if (obj != null) {
    65. if (Date.class.isInstance(obj)) {
    66. return new SimpleDateFormat(FORMAT_DATETIME).format((Date) obj);
    67. } else if (String.class.isInstance(obj)) {
    68. return obj.toString();
    69. } else if (Double.class.isInstance(obj)) {
    70. DecimalFormat df = new DecimalFormat("0.00");
    71. return df.format((Double) obj);
    72. } else if (Integer.class.isInstance(obj)) {
    73. return obj.toString();
    74. }
    75. }
    76. return null;
    77. }
    78. }

    (三)总结

    在客户调用我方发布的接口时产生以下问题:

    javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    起初认为是keystore文件的原因,查看后排除:

    keytool -list -v -keystore D:\Workspaces\rssmcINT\src\tomcat.keystore -storepass pwd**

    确定原因:我方环境是JDK8  TLSv1.2、对方为JDK6 TLSv1.1,以至于低版本无法调用高版本(公司要求我方不能兼容 TLSv1.1)

    1. <Connector port="8080" maxHttpHeaderSize="8192" protocol="HTTP/1.1" SSLEnabled="true"
    2. connectionTimeout="5000" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    3. enableLookups="false" disableUploadTimeout="true" ConnectionTimeout="3000"
    4. acceptCount="100" scheme="https" secure="true"
    5. keystoreFile="D:\Workspaces\rssmcINT\src\tomcat.keystore"
    6. keystorePass="runstone"
    7. clientAuth="false"
    8. sslProtocol="TLS" sslEnabledProtocols="TLSv1.2"
    9. allowUnsafeLegacyRenegotiation="false"
    10. ciphers="TLS_ECDHE_RSA_WITAES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AE
    11. S_256_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA"/>

    解决方案:

    (1)调用方升级JDK 8

    (2)做证书认证绕过

  • 相关阅读:
    win10 GTX 1650 版本517(需要降级到441.22) 安装Tensorflow-GPU
    Redis
    【Plus】三、BaseMapper
    记录下跑VUE+webpack-dev-server安装使用的问题
    Python第四次作业
    关于设置MySQL中create_time和update_time默认值和实时更新
    JavaScript设计模式(五)——发布订阅模式、桥接模式、组合模式
    在Ubuntu上安装LibRaw
    软考中级系统集成项目管理工程师自学好不好过,怎么备考,给点经验
    内衣洗衣机有必要买吗?口碑好的小型洗衣机测评
  • 原文地址:https://blog.csdn.net/qq_39028580/article/details/125564600