首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

运用CXF作为WEBSERVICE的服务端,用AXIS进行调用

2012-09-05 
使用CXF作为WEBSERVICE的服务端,用AXIS进行调用转自:http://haroldxie.iteye.com/blog/751519?当使用CXF作

使用CXF作为WEBSERVICE的服务端,用AXIS进行调用

转自: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" />
?<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" />
?<bean id="loggingInInterceptor" />
?<bean id="ipInterceptor" />
?<bean id="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" }));

?}
}

?

通过这种方式,客户端不需要关心服务端采用了哪种实现方式!

?

热点排行