增加CXF依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>${cxf.version}</version> <type>pom</type> </dependency>
创建服务接口
import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHi(String text); } import javax.jws.WebService; @WebService public class HellowWordController implements HelloWorld { /* * (non-Javadoc) * * @see com.wfj.infc.HelloWorld#sayHi(java.lang.String) */ public String sayHi(String text) { // TODO Auto-generated method stub System.out.println("sayHi called"); return "Hello " + text; } }
cxfdemo-beans.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" default-autowire="byName"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="hello" class="com.wangfj.product.core.controller.HellowWordController"/> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/webservice/hw"/> </beans>
在webapp中发布
XF提供了spring的集成,同时还提供了org.apache.cxf.transport.servlet.CXFServlet用于在 web容器中发布WebService。 前面的例子中增加了整个apache-cxf的依赖,所以会自动增加对srping的引用。只需要写beans配置文件和web.xml文件即可。
- 在web.xml中配置CXFServlet
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cxfdemo-beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
时间: 2024-10-05 00:19:46