Browse Source

ws-client

一般的电脑1 5 years ago
parent
commit
b476fcea53

+ 10 - 0
pom.xml

@@ -259,6 +259,16 @@
                     <skipTests>true</skipTests>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 

+ 19 - 7
src/main/java/com/miyzh/controller/WsController.java

@@ -27,9 +27,9 @@ public class WsController {
         try{
             System.out.println("GetIn");
             String webUrl = "http://15.72.29.170:86/qdmmib/webservice/runservice?wsdl";
-            String methodName = "uploadPatBillInfo";
+            String methodName = "runservice";
             String param = "<Request>" +
-            "<TradeCode></TradeCode>" +
+            "<TradeCode>uploadPatBillInfo</TradeCode>" +
             "<Version></Version>" +
             "<YLJGDM></YLJGDM>" +
             "<YLJGXZQH></YLJGXZQH>" +
@@ -114,7 +114,7 @@ public class WsController {
             "<BillDetailList>" +
             "<BillDetail>" +
             "<BDetailNo>44545</BDetailNo>" +
-            "<BNo></ BNo >" +
+            "<BNo></BNo >" +
             "<DOC_NUMBER></DOC_NUMBER>" +
             "<ITEM_DATE>2012-02-02</ITEM_DATE>" +
             "<ITEM_ID>1</ITEM_ID>" +
@@ -140,7 +140,7 @@ public class WsController {
                     "</BillDetail>" +
             "<BillDetail>" +
             "<BDetailNo>44546</BDetailNo>" +
-            "<BNo></ BNo >" +
+            "<BNo></BNo >" +
             "<DOC_NUMBER></DOC_NUMBER>" +
             "<ITEM_DATE>2012-02-02</ITEM_DATE>" +
             "<ITEM_ID>1</ITEM_ID>" +
@@ -165,7 +165,7 @@ public class WsController {
             "<DocLevel></DocLevel>" +
             "</BillDetail>" +
             "</BillDetailList>" +
-            "<Request>";
+            "</Request>";
             return wsClientService.callWebSV(webUrl, methodName, param);
         } catch (Exception e) {
             e.printStackTrace();
@@ -177,7 +177,7 @@ public class WsController {
     public String uploadDocStatus() throws Exception {
         try{
             String webUrl = "http://15.72.29.170:86/qdmmib/webservice/runservice?wsdl";
-            String methodName = "uploadDocStatus";
+            String methodName = "runservice";
             String param =
             "<Request>" +
                 "<TradeCode>uploadDocStatus</TradeCode>" +
@@ -189,7 +189,7 @@ public class WsController {
                 "<JYSJ></JYSJ>" +
                 "<SERIAL_NUMBER>202003141202022</SERIAL_NUMBER>" +
                 "<STATUS>0</STATUS>	" +
-            "<Request>";
+            "</Request>";
             return wsClientService.callWebSV(webUrl, methodName, param);
         } catch (Exception e) {
             e.printStackTrace();
@@ -197,6 +197,18 @@ public class WsController {
         return null;
     }
 
+    @RequestMapping("test")
+    public String test() throws Exception {
+        try{
+            String webUrl = "http://zs2.sjtt.com.cn/webservice/WebService.asmx?wsdl";
+            String methodName = "AgentQuery";
+            String param = "青岛";
+            return wsClientService.callWebSV(webUrl, methodName, param);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
     public static void main(String[] args) {
         WsClientService wsClientService = new WsClientService();

+ 20 - 2
src/main/java/com/miyzh/service/WsClientService.java

@@ -1,9 +1,14 @@
 package com.miyzh.service;
 
+import com.miyzh.utils.JaxWsDynamicClientFactory;
 import org.apache.cxf.endpoint.Client;
-import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.service.model.BindingInfo;
+import org.apache.cxf.service.model.BindingOperationInfo;
 import org.springframework.stereotype.Service;
 
+import javax.xml.namespace.QName;
+
 /**
  * description: WsService
  * date: 2020/6/16 9:40
@@ -15,10 +20,23 @@ public class WsClientService {
     public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
         Client client = dcf.createClient(wsdUrl);
+        //处理 WebService接口和实现类namespace不同的情况
+        // CXF动态客户端在处理此问题时,会报No operation was found with the name的异常
+        Endpoint endpoint = client.getEndpoint();
+        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operationName);
+        BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
+        if (bindingInfo.getOperation(opName) == null) {
+            for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
+                if (operationName.equals(operationInfo.getName().getLocalPart())) {
+                    opName = operationInfo.getName();
+                    break;
+                }
+            }
+        }
         //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
         Object[] objects;
         // invoke("方法名",参数1,参数2,参数3....);
-        objects = client.invoke(operationName, params);
+        objects = client.invoke(opName, params);
         return objects[0].toString();
     }
 

+ 62 - 0
src/main/java/com/miyzh/utils/JaxWsDynamicClientFactory.java

@@ -0,0 +1,62 @@
+package com.miyzh.utils;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.CXFBusFactory;
+import org.apache.cxf.endpoint.EndpointImplFactory;
+import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
+import org.apache.cxf.jaxws.support.JaxWsEndpointImplFactory;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * description: JaxWsDynamicClientFactory
+ * date: 2020/6/17 14:47
+ * author: SZQ
+ */
+public class JaxWsDynamicClientFactory extends DynamicClientFactory {
+
+    protected JaxWsDynamicClientFactory(Bus bus) {
+        super(bus);
+    }
+
+    @Override
+    protected EndpointImplFactory getEndpointImplFactory() {
+        return JaxWsEndpointImplFactory.getSingleton();
+    }
+
+    protected boolean allowWrapperOps() {
+        return true;
+    }
+
+    public static JaxWsDynamicClientFactory newInstance(Bus b) {
+        return new JaxWsDynamicClientFactory(b);
+    }
+
+    public static JaxWsDynamicClientFactory newInstance() {
+        Bus bus = CXFBusFactory.getThreadDefaultBus();
+        return new JaxWsDynamicClientFactory(bus);
+    }
+
+    /**
+     * 覆写父类的该方法<br/>
+     * 注:解决此(错误:编码GBK的不可映射字符)问题
+     *
+     * @return
+     */
+    @Override
+    protected boolean compileJavaSrc(String classPath, List<File> srcList, String dest) {
+        org.apache.cxf.common.util.Compiler javaCompiler
+                = new org.apache.cxf.common.util.Compiler();
+
+        // 设置编译编码格式(此处为新增代码)
+        javaCompiler.setEncoding("UTF-8");
+
+        javaCompiler.setClassPath(classPath);
+        javaCompiler.setOutputDir(dest);
+        javaCompiler.setTarget("1.8");
+
+        return javaCompiler.compileFiles(srcList);
+    }
+
+}