cxf+spring实现webservice

本文详细介绍了如何使用Apache CXF框架搭建WebService服务。包括添加Maven依赖、配置Spring上下文、定义接口及实现类、配置web.xml以及访问验证步骤。

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属性指定

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值