项目非常简单,项目结构如下:
就一个HelloWorldDao接口,一个实现类HelloWorldImpl,一个spring的配置文件applicationContext-server.xml,一个web项目的配置文件web.xml。(当然需要导入CXF3.0.4的包,以及spring的相关包,还有一些像common-logging,aopalliance这样的web项目必备的包,顺便提一下,我的开发环境是jdk1.8.0_40,tomcat7.0.59,我一般喜欢用最新版的!)具体包的截图如下:()
接下来就是spring的包:(其实spring的包也用不完,如和aop相关的)
接下来看代码和配置文件部分:
1:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
2:applicationContext-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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="helloWorldImpl" class="com.java.service.impl.HelloWorldImpl" />
<!-- 2:通过jaxws:server方式来配置webservice -->
<jaxws:server serviceClass="com.java.service.HelloWorldDao"
address="/helloworld">
<jaxws:serviceBean>
<ref bean="helloWorldImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
3:HelloWorldDao
package com.java.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorldDao {
public void sayHello(@WebParam(name = "name") String name);
}
4:HelloWorldImpl
package com.java.service.impl;
import javax.jws.WebService;
import com.java.service.HelloWorldDao;
@WebService
public class HelloWorldImpl implements HelloWorldDao {
@Override
public void sayHello(String name) {
System.out.println("hello," + name);
}
}
在网上查了很多资料,CXF以前的版本需要引入3个xml文件,但是通过测试,只需要引入
<import resource="classpath:META-INF/cxf/cxf.xml" />
即可!
最后进行测试:
注意,我们在web.xml中配置了webservice的拦截器:
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
即凡是与/webservice开头的url都可以交给CXFservice处理。于是在tomcat下面启动项目后,打开浏览器,输入:
http://localhost:8080/CXF3.0.4_Webservice/webservice
结果如下:
点击wsdl后面的超链接,结果如下:
测试成功。
时间: 2024-10-16 14:50:52