1、添加cxf maven依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
2、配置spring-context
<!-- 增加ws schema -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 发布ws -->
<jaxws:endpoint id="wsCheckService" implementor="com.webservice.check.WsCheckServiceImpl" address="/check"/>
3、定义ws接口和实现类
/** 接口 */
import javax.jws.WebService;
@WebService(targetNamespace = "com.webservice.check")
public interface WsCheckService {
String check();
}
/** 实现类 */
import javax.jws.WebService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@WebService(serviceName = "wsCheckService", targetNamespace = "com.webservice.check", endpointInterface = "com.webservice.check.WsCheckService")
@Service
public class WsCheckServiceImpl implements WsCheckService {
private static final Logger logger = LoggerFactory.getLogger(WsCheckServiceImpl .class);
@Override
public String check() {
logger.info("webservice ok");
return "webService OK";
}
}
4、web.xml增加ws拦截
<servlet>
<description>CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
5、浏览器访问 http://localhost:8080/ws/check?wsdl
6、如果需要指定发布地址(指定域名或者是发布到nginx),则在<jaxws:endpoint> publishedEndpointUrl属性指定
本文详细介绍了如何使用Apache CXF框架搭建WebService服务。包括添加Maven依赖、配置Spring上下文、定义接口及实现类、配置web.xml以及访问验证步骤。
800

被折叠的 条评论
为什么被折叠?



