Spring整和CXF发布一个webservice服务

1.在项目中导入webservice的依赖包

  <!-- apache-CXF-frontend -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${apache-cxf.frontend.version}</version>
        </dependency>
        <!-- apache-cxf-transports -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${apache-cxf-transports.version}</version>
        </dependency>

2.在web.xml中配置CXF的servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>bos-web</display-name>
  <!--加载spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <!--添加监听spring容器的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置CXF的前端控制器 -->
  <servlet>
    <servlet-name>crm-cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>crm-cxf</servlet-name>
    <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

</web-app>

3.在service层写接口

@WebService
public interface CustomerService {
	@WebMethod
	List<Customer> getCustomerList() throws Exception;
}

写接口的实现

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public List<Customer> getCustomerList() throws Exception {
		List<Customer> list = customerMapper.selectByExample(new CustomerExample());
		return list;
	}

}

4.在cxf的applicationContext-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:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd
					http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd
					">

	
	<!--  Cxf WebService 服务端示例 -->
	<!-- 
		implementor:该服务的实现类
		address:进入cxf的前端控制器后访问该服务的地址
	 -->
    <jaxws:endpoint id="customerService" implementor="com.jujung.bos.service.impl.CustomerServiceImpl" address="/customerService"/>
</beans>

 5.启动应用服务器,在地址栏输入 http://localhost:8081/service/customerService?wsdl就可以访问

我配置的tomcat插件是

 <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <path>/</path>
                    <port>8081</port>
                </configuration>
            </plugin>

猜你喜欢

转载自blog.csdn.net/ZQQ8015/article/details/85764967