转自:http://haroldxie.iteye.com/blog/751519
当使用CXF作为WEBSERVICE的服务端,用AXIS进行调用的时候,发现与正常的WSDL文件缺少参数的定义和返回类型定义;需要在服务端的类中进行设置;如下:
@WebService
public interface HelloWorld {
@WebResult(name = "String")
String sayHello(@WebParam(name = "name") String name,
@WebParam(name = "sex") String sex);
void test();
}
@WebService
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name, String sex) {
if ("F".equals(sex)) {
return "Hello," + name + "小姐";
}
if ("M".equals(sex)) {
return "Hello," + name + "先生";
} else {
return "Hello," + name;
}
}
public void test() {
System.out.println("only test for privilege!");
}
}
将类编写好后,只需要在服务端的配置中定义;如下:
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<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" />
<bean id="hello" class="com.cigna.cmc.cxf.service.impl.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloWorld">
<jaxws:inInterceptors>
<ref bean="ipInterceptor" />
<ref bean="saajInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="loggingOutInterceptor" />
</jaxws:outInterceptors>
<jaxws:inFaultInterceptors>
<ref bean="loggingInInterceptor" />
</jaxws:inFaultInterceptors>
<jaxws:outFaultInterceptors>
<ref bean="loggingOutInterceptor" />
</jaxws:outFaultInterceptors>
</jaxws:endpoint>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="ipInterceptor" class="com.cigna.cmc.spring.interceptor.IPInterceptor" />
<bean id="saajInInterceptor" class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />
</beans>
这样服务端的所有设置就都完成了,在客户端进行调用时,采用AXIS的方式,如下:
public class TestHelloWorld {
public static void main(String[] args) throws Exception {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress("http://localhost:8080/CFXDemoServer/services/HelloWorld");
call.setOperationName("sayHello");
call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("sex", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
System.out.println(call.invoke(new Object[] { "harold!","l" }));
}
}
通过这种方式,客户端不需要关心服务端采用了哪种实现方式!