<jaxws:endpoint id="xxWebService" implementor="#firstCxfService" address="/xxWebService" />
别忘了address中的 / 非常重要
package com.controller;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.webservice.FirstCxfService;
import com.webservice.bo.TestBo;
/**
* @author:
* @TODO:动态wsdl文件
*/
public class CxfClient {
private static final String WSDL_URL = "http://localhost:8080/RestFulServer/services/xxWebService?wsdl";
public static void main(String[] args) {
// 第一种方式需要客户端生成wsdl对应的java文件 (非动态)
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
// 获取服务器端
factoryBean.setAddress(WSDL_URL);
// 通过客户端的接口获取服务器端的接口
factoryBean.setServiceClass(FirstCxfService.class);
FirstCxfService hello = (FirstCxfService) factoryBean.create();
System.out.println(hello.sayHello(2));
TestBo testBo = hello.getTestBo("lau");
System.out.println(testBo.getName());
// 第二种方式 不需要wsdl文件生成java文件 (动态)
try {
JaxWsDynamicClientFactory dynamicClientFactory = JaxWsDynamicClientFactory
.newInstance();
Client client = dynamicClientFactory
.createClient(WSDL_URL);
// 第一个参数是方法名 第二个参数是方法对应的参数值
Object[] objects = client.invoke("sayHello", 2);
System.out.println(objects[0].toString());
objects = client.invoke("getTestBo", "lau");
//得到对象所封装的值
System.out.println(objects[0].getClass().getMethod("getName").invoke(objects[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果动态调用出差则可能由下面的原因导致
1. 注意:如果使用动态调用wsdl文件则应该在定义webService时指定命名空间
命名空间的值与wsdl文件中的命名空间想对应
2.产生的原因是没有获得编译环境, 也就是JRE设置的问题, 需要在eclipse里面把jre设置为jdk下的jre。
package com.webservice.impl;
import javax.annotation.Resource;
import javax.jws.WebService;
import com.dwr.Test;
import com.dwr.TestDAO;
import com.webservice.FirstCxfService;
import com.webservice.bo.TestBo;
//指定命名空间targetNamespace
@WebService(targetNamespace = "http://webservice.com/",
endpointInterface = "com.webservice.FirstCxfService")
public class FirstCxfServiceImpl implements FirstCxfService {
@Resource
private TestDAO testDAO;
@Override
public int sayHello(int id) {
Test test = new Test();
test.setId(id);
return this.testDAO.getCount(test);
}
@Override
public TestBo getTestBo(String name) {
TestBo testBo = new TestBo();
testBo.setName(name);
return testBo;
}
}
WSDL文件中的wsdl:definitions标签下的targetNamespace属性值对应
<?xml version='1.0' encoding='UTF-8'?> <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="FirstCxfServiceImplService" targetNamespace="http://webservice.com/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" elementFormDefault="unqualified" targetNamespace="http://webservice.com/" version="1.0"> <xs:element name="getTestBo" type="tns:getTestBo"/> <xs:element name="getTestBoResponse" type="tns:getTestBoResponse"/> <xs:element name="sayHello" type="tns:sayHello"/> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/> <xs:complexType name="sayHello"> <xs:sequence> <xs:element name="arg0" type="xs:int"/> </xs:sequence> </xs:complexType> <xs:complexType name="sayHelloResponse"> <xs:sequence> <xs:element name="return" type="xs:int"/> </xs:sequence> </xs:complexType> <xs:complexType name="getTestBo"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="getTestBoResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="tns:testBo"/> </xs:sequence> </xs:complexType> <xs:complexType name="testBo"> <xs:sequence> <xs:element minOccurs="0" name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello"> <wsdl:part element="tns:sayHello" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getTestBo"> <wsdl:part element="tns:getTestBo" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getTestBoResponse"> <wsdl:part element="tns:getTestBoResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="FirstCxfService"> <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello"> </wsdl:input> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="getTestBo"> <wsdl:input message="tns:getTestBo" name="getTestBo"> </wsdl:input> <wsdl:output message="tns:getTestBoResponse" name="getTestBoResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="FirstCxfServiceImplServiceSoapBinding" type="tns:FirstCxfService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getTestBo"> <soap:operation soapAction="" style="document"/> <wsdl:input name="getTestBo"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="getTestBoResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="sayHello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="sayHelloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="FirstCxfServiceImplService"> <wsdl:port binding="tns:FirstCxfServiceImplServiceSoapBinding" name="FirstCxfServiceImplPort"> <soap:address location="http://localhost:8080/RestFulServer/services/xxWebService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>