用cxf生成的方式,开发web service应用
这次开发web service对接,用的是cxf2.0.13
本来只用了cxf的框架,没有使用cxf提供的工具,比如wsdl2java等
这种方式比较简单,主要是根据得到的wsdl文件,手工写接口类以及model类。详细说明见另外一篇博客:http://kyfxbl.iteye.com/blog/1432952
不过发现这种方式生成的wsdl,和原始的wsdl文件有些不一样,担心真正联调的时候跑不通,所以准备了另一套替代方案,就是用wsdl2java工具,来生成代码,保证wsdl完全一致
1、首先用wsdl2java命令,生成所需的java文件,当然前提是已经得到了wsdl文件
wsdl2java -p com.huawei.wfm.czekh.webservice.remedy -d ../../src/remedy -all fileName.wsdl
用上面这个命令,就可以根据wsdl文件,生成所需的所有java文件,参数说明如下:
-p 指定生成java文件的package name
-d 生成java文件的存放路径
-all 生成客户端和服务端代码,这里还可以用-client生成客户端,用-server生成服务端,不过实际上区别不大,只要用-all就可以了
最后一个参数就是目标wsdl文件
2、查看生成的文件列表,可以看到有以下文件:
大部分都是model类,可以不管。以下文件是比较特殊的:
ObjectFactory.java
package-info.java
这2个文件我也不知道是干嘛的,不过反正是不能删的
以_Client结尾的那个文件,是一个客户端,没有用,可以删除
以_Server结尾的那个文件,是启动服务端(貌似是内置的jetty),没有用,可以删除
以Service结尾的那个文件,不但没有用,还要坏事,因为其中有以下代码:
@WebServiceClient(name = "CIP-B2B_ServiceAssuranceWorkForceClientManagementService", wsdlLocation = "file:CIP-B2B_ServiceAssuranceWorkForceClientManagement.wsdl", targetNamespace = "http://cz.o2.com/systems/integrationinfrastructure/CIP-B2B/CIP-B2B_ServiceAssuranceWorkForceClientManagement/1.0")
@javax.jws.WebService( serviceName = "CIP-B2B_ServiceAssuranceWorkForceClientManagementService", portName = "CIP-B2B_ServiceAssuranceWorkForceClientManagementPort", targetNamespace = "http://cz.o2.com/systems/integrationinfrastructure/CIP-B2B/CIP-B2B_ServiceAssuranceWorkForceClientManagement/1.0", wsdlLocation = "file:CIP-B2B_ServiceAssuranceWorkForceClientManagement.wsdl", endpointInterface = "com.huawei.wfm.czekh.webservice.remedy.CIPB2BServiceAssuranceWorkForceClientManagementPortType")
wsdlLocation = "file:CIP-B2B_ServiceAssuranceWorkForceClientManagement.wsdl",
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><context:component-scan base-package="com.huawei.wfm.czekh" /><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="remedy" implementor="#MyWebserviceServer"address="/RemedyWebService" /></beans>
@Component("MyWebserviceServer")@WebService(serviceName = "CIP-B2B_ServiceAssuranceWorkForceClientManagementService", portName = "CIP-B2B_ServiceAssuranceWorkForceClientManagementPort", targetNamespace = "http://cz.o2.com/systems/integrationinfrastructure/CIP-B2B/CIP-B2B_ServiceAssuranceWorkForceClientManagement/1.0", endpointInterface = "com.huawei.wfm.czekh.webservice.remedy.CIPB2BServiceAssuranceWorkForceClientManagementPortType")public class CIPB2BServiceAssuranceWorkForceClientManagementPortTypeImplimplements CIPB2BServiceAssuranceWorkForceClientManagementPortType {@Autowiredprivate Test test; // 省略无关方法public AcknowledgeResponse acknowledge(AcknowledgeRequest body) {test.sayHi("somebody");AcknowledgeResponseBody responseBody = new AcknowledgeResponseBody();responseBody.setStatus(true);responseBody.setErrorDescription("wcnm");AcknowledgeResponse result = new AcknowledgeResponse();result.setResponseBody(responseBody);return result;}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="propertyConfigurer"/></beans>
public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("cxf.xml");CIPB2BServiceAssuranceWorkForceClientManagementPortType client = (CIPB2BServiceAssuranceWorkForceClientManagementPortType) context.getBean("client");AcknowledgeRequestBody body = new AcknowledgeRequestBody();body.setCustSysId("123456");AcknowledgeRequest request = new AcknowledgeRequest();request.setRequestBody(body);AcknowledgeResponse response = client.acknowledge(request);String desc = response.getResponseBody().getErrorDescription();System.out.println(desc);}}