兩種實現:
先定義一個服務接口:
/** * webservice 服務接口 * @author hkk */ public interface IServiceAgent { String invoke(Object... params) throws Exception; }
一. cfx實現:
/** * WebService訪問代理類:cxf */ @Component @Primary @Slf4j public class WebServiceCFXAgent implements IServiceAgent { /** * 創建動態客戶端工廠 */ private CisJaxWsDynamicClientFactory dynamicClientFactory; /** * 創建動態客戶端 */ private Client client; /** * 配置文件Bean,獲取webservice url */ private AppConfig appConfig; public WebServiceCFXAgent(AppConfig appConfig) { this.appConfig = appConfig; initConfig(); } private void initConfig() { this.dynamicClientFactory = CisJaxWsDynamicClientFactory.newInstance(); this.client = dynamicClientFactory.createClient(appConfig.getServiceUrl()); JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); // 設置超時時間 HTTPConduit conduit = (HTTPConduit) this.client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); //連接超時時間:30s httpClientPolicy.setConnectionTimeout(30000); //請求響應超時時間:60s httpClientPolicy.setReceiveTimeout(60000); conduit.setClient(httpClientPolicy); } /** * Webservice調用方法 * @param methodName * @param params * @return * @throws Exception */ public String invoke(String methodName, Object... params) throws Exception { Object[] objects = new Object[0]; try { objects = this.client.invoke(methodName, params); return objects[0].toString(); } catch (Exception ex){ log.error("上傳報文:" + Arrays.toString(params)); log.error("返回報文:" + Arrays.toString(objects)); log.error(ex.getMessage()); throw ex; } } /** * Webservice調用方法 * @param params * @return * @throws Exception */ @Override public String invoke(Object... params) throws Exception { return invoke(appConfig.getMethodName(), params); } }
二. axis2實現
/** * WebService訪問代理類:axis2 */ @Component public class WebServiceAxis2Agent implements IServiceAgent { private RPCServiceClient serviceClient; private Class[] responseParam; private QName requestMethod; /** * 配置文件Bean,獲取webservice url */ private AppConfig appConfig; public WebServiceAxis2Agent(AppConfig appConfig) throws AxisFault { this.appConfig = appConfig; serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調用WebService的URL EndpointReference targetEPR = new EndpointReference(appConfig.getServiceUrl()); options.setTo(targetEPR); // 指定方法返回值的數據類型的Class對象 responseParam = new Class[] { String.class }; // 指定要調用的getGreeting方法及WSDL文件的命名空間 requestMethod = new QName(appConfig.getNamespaceUri(), appConfig.getMethodName()); } @Override public String invoke(Object... params) { String result = null; try { // 調用方法並輸出該方法的返回值 result = "" + serviceClient.invokeBlocking(requestMethod, params, responseParam)[0]; } catch (AxisFault e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return result; } }
以上的兩種方法調用方法都是一樣:通過接口注入,注意@Primary,將優先使用
@Autowired private IServiceAgent webAgent; @RequestMapping("/test") public String webServiceTest() throws Exception { Object[] objs = {"2343", "435", "567", "678", "789", "789"}; String result = webAgent.invoke(objs); return result; }
這個調用invoke方法其實不太友好,所以
/** * WebService訪問代理類:cxf * @author hkk */ @Configuration @Slf4j public class WebServiceCFXConfiguration { /** * 配置文件Bean,獲取webservice url */ private AppConfig appConfig; public WebServiceCFXConfiguration(AppConfig appConfig) { this.appConfig = appConfig; } @Bean public Mhs5service mhs5service() { JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(Mhs5service.class); factoryBean.setAddress(appConfig.getServiceUrl()); Mhs5service mhs5service = (Mhs5service) factoryBean.create(); // 設置超時時間 // HTTPConduit conduit = (HTTPConduit) this.client.getConduit(); // HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); // //連接超時時間:30s // httpClientPolicy.setConnectionTimeout(30000); // //請求響應超時時間:60s // httpClientPolicy.setReceiveTimeout(60000); // conduit.setClient(httpClientPolicy); return mhs5service; } }
接口類定義:
@WebService(targetNamespace = "http://service.server.hkk.com/") public interface Mhs5service { @WebMethod(operationName="invokeCotton") String invokeCotton(String a, String b, String c, String d, String e, String f); }
注意要加:targetNamespace
這個調用就友好多了
@Autowired private Mhs5service mhs5service; @RequestMapping("/mhs5service") public String mhs5service() throws Exception { String result = mhs5service.invokeCotton("2343", "435", "567", "678", "789", "789"); return result; }
POM文件:
<properties> <java.version>1.8</java.version> <org.apache.axis2.version>1.7.8</org.apache.axis2.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <ojdbc8.version>12.2.0.1</ojdbc8.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.2</version> </dependency> <!--配置oracle數據庫,從maven倉庫中獲取--> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>${ojdbc8.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.7</version> </dependency> <!--axis2 begin--> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-spring</artifactId> <version>${org.apache.axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-http</artifactId> <version>${org.apache.axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-local</artifactId> <version>${org.apache.axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-xmlbeans</artifactId> <version>${org.apache.axis2.version}</version> </dependency> <!--axis2 end--> </dependencies>