1.
新建一个JavaWebProject,命名为cxfDemo
选择【next】,为project添加userLib库。
2.
打开新建的project,在src下新建包com.handou.cxf.test。在包中新建inteface名称为HelloWorld
代码如下:
@WebService
public
interface HelloWorld {
@WebMethod
public String
sayHello(@WebParam(name="name")String name);
}
新建一个class为该接口的实现类,其代码如下:
public class HelloWorldImpl
implements HelloWorld {
public String
sayHello(String name) {
//
TODO Auto-generated method
stub
return name.concat(" ,
Hello WebService!!!");
}
}
3.
发布webservice。
采用cxf内置的Jetty服务器发布服务。在HelloWorldImpl的实现类中添加main()方法,具体如下:
public
static void main(String[] args) {
HelloWorldImpl implementor = new
HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new
JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:8080/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new
LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new
LoggingOutInterceptor());
svrFactory.create();
}
4. 在地址栏输入http://localhost:8080/helloWorld?wsdl,查看wsdl详细信息
5.
访问webservice服务端。
方法1:由wsdl可知接口只有一个供调用的operation,方法名称为sayHello,输入参数变量名称为name,数据类型为string,采用地址栏url?+参数测试,输入:
http://localhost:8080/helloWorld/sayHello?name=HanDou,显示返回信息如图:
方法2:客户端编码调用。
新建Java类HelloClient,其代码如下:
public class HelloClient {
/**
* @param
args
*/
public
static void main(String[] args)
{
JaxWsProxyFactoryBean factory = new
JaxWsProxyFactoryBean();
//factory.getInInterceptors().add(new
LoggingInInterceptor());
factory.getOutInterceptors().add(new
LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8080/helloWorld");
HelloWorld client = (HelloWorld)
factory.create();
String
reply =
client.sayHello("HanDou");
System.out.println("Server said: " +
reply);
System.exit(0);
}
}
控制台显示反馈消息:
6.
采用spring+cxf集成发布webservice。
1.在WEB-INF下新建beans.xml,beans.xml中添加cxf配置。
<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" />
<jaxws:endpoint
id="helloWorld"
implementor="com.handou.cxf.test.HelloWorldImpl"
address="/HelloWorld"/>
</beans>
2.在web.xml中添加spring和cxf的配置
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF
Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
3.部署文件至服务器,启动服务器,通过IP地址+端口+工程名+webservice服务名。
先访问工程,如图:
点击wsdl进入http://localhost:8080/cxf/HelloWorld?wsdl,可获得wsdl详细信息