webservice

lishihuan大约 3 分钟

webservice

springboot+cfx实现webservice功能

参考:https://blog.csdn.net/weixin_42924812/article/details/106671633open in new window

https://blog.csdn.net/qq_16055765/article/details/90084200open in new window -- 讲到拦截的概念

客户端

/**
	 * 方式1:动态调用方式
	 */
	@Scheduled(cron="*/5 * * * * ?")
	public String getMessage4()  {
		//Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
		System.out.println("======开始调用webservice接口-动态调用方式=====");
		String url = "http://localhost:8082/cpmsapi/webService/IfaceService?wsdl";
		String methodName = "findOffice";
		System.out.println("Calling" + url);
		String result="";
		try {
			String[] parameters = new String[]{"",""};
			result=clientUtil.callWebSV(url, methodName, "","");
		} catch (Exception e) {
			e.printStackTrace();
			log.info("接口调用失败{} "+e.getMessage());
			return  "失败";
		}
		log.info("===Finished!===恭喜你啊!!!IfaceService接口调用成功!!!===获得的数据是:"+result);
		System.out.println("===Finished!===恭喜你啊!!!IfaceService接口调用成功!!!===获得的数据是:"+result);
		return "Finished!";
	}
	/**
	 * 方式2.代理类工厂方式,需要拿到对方的接口
	 */
	//@Scheduled(cron="*/5 * * * * ?")
	public static void method2() {
		try {
			String url = "http://localhost:8082/cpmsapi/webService/IfaceService?wsdl";
			// 代理工厂
			JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
			// 设置代理地址
			jaxWsProxyFactoryBean.setAddress(url);
			// 添加用户信息验证
			//jaxWsProxyFactoryBean.getOutInterceptors().add( new ClientInterceptor("admin", "123456"));
			// 设置接口类型
			jaxWsProxyFactoryBean.setServiceClass(IfaceService.class);
			// 创建一个代理接口实现
			IfaceService ws = (IfaceService) jaxWsProxyFactoryBean.create();
			// 调用代理接口的方法调用并返回结果
			List<Office> office = ws.findOffice("", "");
			System.out.println("===Finished!===恭喜你啊!!!TestService接口调用成功!!!===获得的数据是:"+office);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

常见问题:

1.CXF出现:编码 GBK 的不可映射字符问题解决

复写 DynamicClientFactory.java 中的compileJavaSrc方法

JaxWsDynamicClientFactory.java

参考:https://zhuanlan.zhihu.com/p/272482046open in new window

package com.semdo.cpmsapi.webserver.client.utils;

import org.apache.cxf.endpoint.Client;
//import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;// 本来调用的类,但是 可能出现 GBK 的不可映射字符 问题
import com.semdo.cpmsapi.webserver.client.JaxWsDynamicClientFactory;// 重写DynamicClientFactory

public class clientUtil {
    /**
     * 动态调用方式
     */
	public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();//调用的是 自己重写 DynamicClientFactory的类,否使出现编码 GBK 的不可映射字符
		Client client = dcf.createClient(wsdUrl);
		//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
		Object[] objects;
		// invoke("方法名",参数1,参数2,参数3....);
		objects = client.invoke(operationName, params);
		return objects[0].toString();
	}

}

2. 打jar 包运行报错

调用webservice的时候出现com.sun.tools.internal.xjc.api.XJC

<!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf</artifactId>
            <version>${spring.webservice.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${spring.webservice.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${spring.webservice.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api 
        	为了解决 jar 运行下 报错
        -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.2.11</version>
        </dependency>
        <!-- CXF webservice -->

如果增加这几个jar 还是无法正常运行,还将 tools.jar 拷贝到 与jdk 同级目录下的jre\lib 文件夹下

C:\Program Files\Java\jdk1.8.0_151\lib\tools.jar
-- > 复制tool.jar 到 jre\lib 目录下
C:\Program Files\Java\jre1.8.0_151\lib

3. 无法连接数据库

SpringContextHolder.java

无法通过 @Autowired 注入spring,通过

OfficeService officeService = SpringContextHolder.getBean(OfficeService.class);

如果配置文件定义了port和 项目名称 则访问时需要注意 http://localhost:8082/cpmsapi/webService

 server:
  port: 8082
  servlet:
    context-path: /cpmsapi

HttpURLConnection soap 实现 JaxWsClientUtil.java

http的方式调用webService

Java三种方式实现发送xml参数的WebService接口调用: https://blog.csdn.net/u010479989/article/details/81413023open in new window -- 没测试过

https://blog.csdn.net/weixin_40699910/article/details/103399292open in new window

天气webservice :http://www.webxml.com.cn/zh_cn/weather_icon.aspxopen in new window

springboot 调用webservice https://blog.csdn.net/qq_27471405/article/details/105275657open in new window

webService(二 springboot的wsdl、soap协议)

https://blog.csdn.net/qq_44014971/article/details/105329025open in new window

cfx和 junit 包有冲突

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

springboot项目报错-The Bean Validation API is on the classpath but no implementation could be found

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.6</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>