1.配置apache-cxf-2.7.18环境变量
从Apache网站上下载apache-cxf-2.7.18.jar,如下图所示
下载完成进行环境变量的配置,配置过程如图
这样就配置好了环境变量,我们可以在cmd下进行环境配置是否正确的检测
证明环境配置没有问题
2.在myeclipse中设置需要导入的jar包
增加jar包时把D:\apache-cxf-2.7.18\lib路径下的包都添加进去,如图所示
3.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--
spring需要加载的配置文件
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-cxf.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
cxf服务启动servlet
-->
<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>/webservice/*</url-pattern>
</servlet-mapping>
4.配置spring-cxf.xml文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
xmlns:wsa="http://cxf.apache.org/ws/addressing"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.1.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
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" />
<cxf:bus>
<cxf:features>
<!--日志拦截功能,用于监控soap内容,开发后可以删除 -->
<cxf:logging />
<wsa:addressing />
</cxf:features>
</cxf:bus>
<jaxws:endpoint id="user"
implementor="com.yun.service.User.impl.UserServiceImpl" address="/UserService"
publish="true" />
<jaxws:endpoint id="product"
implementor="com.yun.service.product.impl.ProductServiceImpl" address="/ProductService"
publish="true" />
</beans>
5.编写实现类
目录结构如图
代码部分
service接口层
package com.yun.service.User;
import javax.jws.WebService;
import com.yun.bean.User;
@WebService(targetNamespace="User.service.yun.com")
public interface UserService {
//public String add(String user);
public String sayHi(String text);
public User add(User user);
}
package com.yun.service.product;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace="product.service.yun.com")
public interface ProductService {
public String add(@WebParam(name = "text")String text);
}
service接口层具体实现
package com.yun.service.User.impl;
import javax.jws.WebService;
import com.yun.bean.User;
import com.yun.service.User.UserService;
@WebService(endpointInterface="com.yun.service.User.UserService")
public class UserServiceImpl implements UserService {
public String sayHi(String text) {
// TODO Auto-generated method stub
return "hello,"+text;
}
public User add(User user) {
// TODO Auto-generated method stub
return user;
}
}
package com.yun.service.product.impl;
import javax.jws.WebService;
import com.yun.service.product.ProductService;
@WebService(endpointInterface="com.yun.service.product.ProductService")
public class ProductServiceImpl implements ProductService {
public String add(String text) {
// TODO Auto-generated method stub
return "product:"+text;
}
}
6.测试过程及结果
代码完成后运行tomcat
点击超级连接后
大家在开发过程中遇到问题可以留言