一、简单的(结合Spring)
1、 新建一个web 项目,加入cfx所需要jar
2、 编写要发布的Web Service接口和实现类所需要jar
接口类 HelloWorld :
1 import javax.jws.WebService;
2 @WebService
3 public interface HelloWorld
4 {
5 public String sayHello(String text);
6 }
7 实现类HelloWorldImpl :
8 import javax.jws.WebService;
9 @WebService(endpointInterface="hello.HelloWorld")
10 public class HelloWorldImpl implements HelloWorld
11 {
12 @Override
13 public String sayHello(String text){
14 return "Hello, " + text;
15 }
16 }
实现类HelloWorldImpl :
1 import javax.jws.WebService;
2 @WebService(endpointInterface="hello.HelloWorld")
3 public class HelloWorldImpl implements HelloWorld
4 {
5 @Override
6 public String sayHello(String text){
7 return "Hello, " + text;
8 }
9 }
@WebService 注解表示是要发布的web 服务
3、 在applicationContext_cfx.xml配置要发布的Web Service
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
4 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">
5 <import resource="classpath:META-INF/cxf/cxf.xml" />
6 <!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
7 --> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
8 <bean id="hello" class="hello.HelloWorldImpl" />
9 <jaxws:endpoint id="helloWorld" implementor=" hello.HelloWorldImpl "
10 address="/HelloWorld" />
11 </beans>
注意:<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> id:指在spring配置的bean的ID. Implementor:指明具体的实现类. Address:指明这个web service的相对地址
4、 配置web.xml文件
1 <context-param>
2 <param-name>contextConfigLocation</param-name>
3 <param-value>classpath*:applicationContext*.xml</param-value>
4 </context-param>
5 <listener>
6 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7 </listener>
8 <servlet>
9 <servlet-name>CXFServlet</servlet-name>
10 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
11 <load-on-startup>1</load-on-startup>
12 </servlet>
13 <servlet-mapping>
14 <servlet-name>CXFServlet</servlet-name>
15 <url-pattern>/services/*</url-pattern>
16 </servlet-mapping>
5、 部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/ HelloWorld?wsdl,将显示这个web service的wsdl.
注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> 则访问地址为:http://localhost:8080/<web-app-name>/ws/ HelloWorld?wsdl