一、前言
之前写过用cxf框架中的wsdl2java 根据WSDL文档生成webservice客户端代码
贴中将的这种方式会出现服务器地址是写入代码中的问题。这样对后期的维护是一定的问题。
接下来我们就解决这个问题。方法也很简单。
二、方式
1.配置WSDL文档到本地环境中。
如我的开发环境是使用的MAVEN构建的,所有在resources目录下创建文件夹wsdl
文件夹中保存我们的WSDL文档
wsdl/test.wsdl
这样,在项目环境中的路径就是classpath:wsdl/test.wsdl
2.生成相对路径的代码。
还是使用wsdl2java 这次加入-wsdlLocation 这个参数。
wsdl2java -encoding utf-8 -p com.jeiao.boss -impl -wsdlLocation classpath:wsdl/test.wsdl http://127.0.0.1:9999/boss/test?wsdl
代码中如下:
达到目的。
三、进阶
实现在测试webservice和实际webservice环境中随意切换。
其实我们使用wsdl文档到本地参数时。可以观察到webservice的服务器地址已经固化到文件中。
<wsdl:service name="TestService"> <wsdl:port name="TestServiceHttpPort" binding="tns:TestServiceHttpBinding"> <wsdlsoap:address location="http://192.168.1.100/boss/TestService"/> </wsdl:port> </wsdl:service>
要实现环境的随意切换,可以使用Maven的Profile ,这个在 Maven 使用profiles filters resources build 打包不同配置开发环境及打包中讲到。
<wsdl:service name="TestService"> <wsdl:port name="TestServiceHttpPort" binding="tns:TestServiceHttpBinding"> <wsdlsoap:address location="${env.config.boss.host}/boss/TestService"/> </wsdl:port> </wsdl:service>配置好此Profile就可以在不同环境中使用了。
另:在cxf和stackoverflow上推荐使用 cxf的maven 插件。我测试过不管用,生成不了。
具体代码如下,有兴趣的同学可以试试,顺便指出我哪出错了。问题地址:http://stackoverflow.com/questions/4455195/how-to-avoid-the-need-to-specify-the-wsdl-location-in-a-cxf-or-jax-ws-generated/39097272#39097272
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.0.10</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/InfoQueryService.wsdl</wsdl>
<wsdlLocation>classpath:wsdl/InfoQueryService.wsdl</wsdlLocation>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
jeiao!