项目1、本项目为struts2项目所以需要配置配置文件
applicationcontex-cxf.xml配置
<!-- 定义具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的 -->
<bean id="webServiceInterface"
class="com.git.easyloan.picc.fpdb.piccservice.impl.WebServiceInterfaceImpl" />
<jaxws:server id="WebServiceInterface"
serviceClass="com.git.easyloan.picc.fpdb.piccservice.impl.WebServiceInterfaceImpl"
address="/WebServiceInterface">
<!-- 要暴露的 bean 的引用,上面定义的bean id -->
<jaxws:serviceBean>
<ref bean="webServiceInterface" />
</jaxws:serviceBean>
</jaxws:server>
接口:
@WebService
public interface WebServiceInterfaceIf {
public String sayHello(@WebParam(name = "arg0") String text);
}
实现类:
@Controller
public class WebServiceInterfaceImpl implements WebServiceInterfaceIf{
public String sayHello(String text) {
System.out.println("***************1");
return "Hello : " + text;
}
}
项目2、
实现类:
import javax.xml.namespace.QName;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;
@Service("tbCsmCustomerService")
public class TbCsmCustomerServiceImpl implements TbCsmCustomerServiceIf{
@Override
public Object selectTbCsmCustomer() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 调用项目1的地址
org.apache.cxf.endpoint.Client client = dcf .createClient("http://localhost:8080/easyloan/ws/WebServiceInterface?wsdl");
// url为调用webService的wsdl地址
QName name = new QName("http://service.piccservice.fpdb.picc.easyloan.git.com/", "sayHello");
// namespace是命名空间,methodName是方法名
String xmlStr = "1";
// paramvalue为参数值
Object[] objects;
try {
objects = client.invoke(name,xmlStr);
System.out.println(objects[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
项目2通过webservice调用项目1。