一、简单的(结合Spring)
1、 新建一个web 项目,加入cfx所需要jar
2、 编写要发布的Web Service接口和实现类所需要jar
接口类 HelloWorld :
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
public String sayHello(String text);
}
实现类HelloWorldImpl :
import javax.jws.WebService;
@WebService(endpointInterface="hello.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
@Override
public String sayHello(String text){
return "Hello, " + text;
}
}
实现类HelloWorldImpl :
import javax.jws.WebService;
@WebService(endpointInterface="hello.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
@Override
public String sayHello(String text){
return "Hello, " + text;
}
}
@WebService 注解表示是要发布的web 服务
3、 在applicationContext_cfx.xml配置要发布的Web Service
<?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="hello.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor=" hello.HelloWorldImpl "
address="/HelloWorld" />
</beans>
注意:<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> id:指在spring配置的bean的ID. Implementor:指明具体的实现类. Address:指明这个web service的相对地址
4、 配置web.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-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>/services/*</url-pattern>
</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