一、新建一个maven项目
二、pom.xml引入依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-api</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>2.5.0</version> </dependency> <!-- 如果跑在tomcat下,下面可以不用 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.5.0</version> </dependency>
三、开发接口类
import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String username); }
四、开发实现类
import javax.jws.WebService; import com.xie.ws.HelloWorld; @WebService(endpointInterface="com.xie.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String username) { System.out.println("Hello,"+username); return "Hello,"+username; } }
五、修改web.xml
<!-- 配置 Spring 配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/config/spring.xml</param-value> </context-param> <!-- 启动 IOC 容器的 ServletContextListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置字符集 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> <!-- 这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/项目/webservice/服务名 --> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
六、添加spring.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" 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"> <jaxws:endpoint id="helloWorld" implementor="com.xie.ws.impl.HelloWorldImpl" address="/helloWorld"/> <!-- <bean id="helloWordImpl" class="com.xie.ws.impl.HelloWordImpl"/> <jaxws:endpoint id="helloWord" implementor="#helloWordImpl" address="/helloWord" /> --> </beans>
七、部署到tomcat启动后访问
浏览器输入: http://localhost:8080/CxfWSServer/webservice/helloWorld?wsdl
时间: 2024-10-05 05:07:40