1.首先你需要創建一個maven項目【當然是web項目】
2.pom.xml添加以下
<properties>
<cxf.version>2.2.3</cxf.version>
</properties>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
沒錯只需要引入這兩個,然后cxf需要的其他jar也會自動添加到項目中。
3.在項目的web.xml中添加
<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>/cxf/*</url-pattern>
</servlet-mapping>
4.具體代碼的實現如下:
4.1寫一個接口IHelloService.java
package com.niuniu.service;
import javax.jws.WebService;
@WebService
public interface IHelloService {
public String wolaile(String userName);
}
4.2寫接口的實現HelloServiceImpl.java
package com.niuniu.service.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.niuniu.service.IHelloService;
@WebService(endpointInterface="com.niuniu.service.IHelloService",serviceName="helloService")
@Component
public class HelloServiceImpl implements IHelloService {
@Override
public String wolaile(String userName) {
return "Hello, wolaile, " + userName;
}
}
4.3配置文件如下:
<?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-3.2.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-servlet.xml" />
<jaxws:endpoint id="helloService" implementor="com.niuniu.service.impl.HelloServiceImpl" address="/cxfService" />
</beans>
spring的相關配置就不在這里敘述了。
【如果沒有搭過spring的web項目的,可以先搜索一個能跑起來的web項目,然后把這些配置放到能跑起來的項目中就可以了】
把項目發布到tomcat上,然后訪問http://localhost:8080/ownweb/cxf/cxfService?wsdl,看下能否成功訪問。
【ownweb是你的項目名,cxf是在web.xml中配置的<url-pattern>,cxfService是4.3中address的值,根據自己的需要可以修改下】