使用apache CXF和maven开发Web Service

本文介绍如何使用Java和CXF框架搭建WebService服务。通过Maven创建项目,并配置CXF依赖及WebService相关注解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写服务类

从Java6开始,WebService API从Java EE复制到了Java SE。并遵循了一系列的标准,比如JSR181(Web Service 元数据),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件标准)等。 并分别定义到javax.jws, javax.xml.ws 和 javax.xml.soap包中。

JSR181支持使用标注(annotation)来定义WebService。在javax.jws中主要的标注类包括:

 
标注 说明
WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
WebMethod 定制Web Service方法
WebParam 定制Web Service方法的参数
WebResult 定制Web Service方法的返回值
SOAPBinding 指定WebService的SOAP映射样式

使用标注可以在不改变代码逻辑的前提下让外部代码能够获得更多的元数据。下面就用javax.jws定义的标注来声明一个WebService:

创建maven工程

mvn archetype:create -DgroupId=com.mycompany -DartifactId=cxfdemo -DarchetypeArtifactId=maven-archetype-webap

增加CXF依赖----  pom.xml

<apache-cxf.version>2.7.6</apache-cxf.version>     版本号 
<dependency> 
             <groupId>org.apache.cxf</groupId> 
             <artifactId>cxf-rt-core</artifactId>
             <version>${apache-cxf.version}</version>
         </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${apache-cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${apache-cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.7</version>
        </dependency>
在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向 OSChina 的 Maven 地址。修改如下内容:
<repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
CXF提供了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>/webService/*</url-pattern>
    </servlet-mapping>

在web.xml中增加spring的ContextLoaderListener并配置context-param
<context-param>
         <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc-webService.xml </param-value>       //配置xml路径 
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
新建 :spring-mvc-webService.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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
    <jaxws:endpoint id="webService" implementor="com.xxx.xxx.api.ws.SendSmsEndpoint" address="/webService" />
</beans>

implementor="com.xxx.xxx.api.ws.SendSmsEndpoint"  为具体的接口实现类

@WebService
public class SendSmsEndpoint {
    @WebMethod
    public String sendSms(){
    }
}

具体的实现方法,就在SendSmsEndpoint中编写

如此,WebService就已经在web容器中发布了。启动web应用:
就可以在浏览器中看到已经发布的WebService,如下图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值