cxf+spring实现webservice

2023-05-16

1、构建maven项目,工程结构如下:

这里需要特别指出就是cxf-core-3.1.12.jar类路径META-INF/cxf下有一个cxf.xml的配置文件,这个在applicationContext.xml配置文件中会使用到。

2、编写pom.xml文件,这里需要引入cxf-rt-frontend-jaxws,cxf-rt-transports-http两个jar,因为要和spring整合,这里还需要引入spring的依赖。下面的pom文件已经是最精简的。

<properties>
          <cxf.version>3.1.12</cxf.version>
          <spring.version>4.2.0.RELEASE</spring.version>
  </properties>
  <dependencies>
          <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>${cxf.version}</version>
          </dependency>
          <dependency>
                 <groupId>org.apache.cxf</groupId>
                 <artifactId>cxf-rt-transports-http</artifactId>
                 <version>${cxf.version}</version>
          </dependency>      
          <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-beans</artifactId>
                  <version>${spring.version}</version>
          </dependency>
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
          </dependency>
          <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${spring.version}</version>
          </dependency>
  </dependencies>

3、接口和实现类:自定义一个webservice接口BaseService并开启注解,自定义BusinessService类实现BaseService接口。

BaseService.java

package com.xxx.service;

import javax.jws.WebService;

@WebService
public interface BaseService {
    public int add(int a,int b);
    public String sayHello(String name);
}

BusinessService.java

package com.xxx.service.impl;

import javax.jws.WebService;

import com.xxx.service.BaseService;
@WebService(endpointInterface="com.xxx.service.BaseService")
public class BusinessService implements BaseService {

    @Override
    public int add(int a, int b) {
	return a+b;
    }

    @Override
    public String sayHello(String name) {
	return "hello,"+name;
    }

}

4、web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>webservice</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
     <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>
            <url-pattern>/webservice/*</url-pattern>
      </servlet-mapping>
</web-app>

5、applicationContext.xml,这里需要指出的是在创建applicationContext.xml之后,在命名空间中引入jaxws,文件中需要引入一个cxf.xml文件,这个文件无需手动创建,他在cxf-core-3.1.12.jar类路径下的META-INF/cfx下。在第一步的项目结构中可以看到。

<?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">

     <!-- 该文件是在cxf-core-3.1.12.jar的类路径下的META-INF/cxf/cxf.xml -->
     <import resource="classpath:META-INF/cxf/cxf.xml"/>
      <bean id="business" class="com.xxx.service.impl.BusinessService"></bean>
      <jaxws:endpoint id="businessService" address="/business" implementor="#business"></jaxws:endpoint>
</beans>

6、接口发布

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cxf+spring实现webservice 的相关文章

随机推荐

  • windows上Flask环境搭建

    Flask是python开发框架 用来快速构建web项目 下面介绍如何在windows上搭建flask开发环境并运行一个demo 第一步 创建项目并构建flask环境 mkdir flaskapp cd flaskapp virtualen
  • WebSocket 测试工具

    一 WebSocket 简介 WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单 xff0c 允许服务端主动向客户端推送数据 在WebSocket API中 xff
  • 利用pipework为docker容器设置固定IP

    今天介绍如何在redhat centos7系列机器上使用pipework为docker启动的容器指定一个固定ip 我们知道默认情况下 xff0c docker会使用bridge网络模式为每一个启动的容器动态分配一个IP xff0c 以172
  • 用docker玩坏ubuntu虚拟机容器

    当我们装上docker之后 xff0c 自然会pull一个或多个镜像玩玩 xff0c 这时候 xff0c docker hub仓库上有很多系列操作系统镜像 xff0c 每个系列又有很多不同功能的虚拟机镜像 xff0c 比如centos分6还
  • tornado入门实例

    tornado是python web开发的又一个轻量级框架 tornado框架需要安装 xff0c 为了方便 xff0c 我直接安装了Anaconda 2 4 1 里面直接就带了tornado 还有很多python库 numpy scipy
  • web.py框架入门

    web py是python web开发的一个轻量级框架 web py可以通过pip命令安装 xff0c pip install web py 编写官网示例代码 xff1a vi index py import web urls 61 34
  • graphviz快速上手

    graphviz最初是AT amp T实验室用来画流程图的工具 xff0c 使用dot语言 其中根据图的类型可以分为有向图 dirgraph 和无向图 graph 我们知道图是由点 node 和边 edge 组成的 xff0c 在有向图中边
  • mysqld: File './mysql-bin.index' not found (Errcode: 13 - Permission denied)

    我们通过yum方式安装mysql 会生成mysql mysql用户组和用户 xff0c 启动mysql默认是使用mysql用户 如果我们开启了慢log日志 xff0c 而且我们使用service mysqld start启动mysql 会报
  • redhat7编译安装php-5.5.38

    1 从官网下载php源码包 php 5 5 38 2 安装依赖包 yum install libxml2 libxml2 devel bzip2 devel libcurl devel y yum install openssl opens
  • spark-1.6.0源码编译安装

    环境准备 spark是scala语言写的 xff0c scala运行需要jdk 如果通过maven编译 xff0c 还需要maven环境 xff0c 因此spark源码编译需要安装jdk scala apache maven这三样环境 这里
  • ZendStudio+php+Apache开发环境搭建

    学习php xff0c 我们就想有一个好的ide xff0c ZendStudio是专门为php开发提供的ide xff0c 写完代码立马能够在工作空间中调试 xff0c 可以通过Run As gt PHP CLI Application
  • 图文详解win7实现局域网共享文件

    工作中 xff0c 我们有时候会拥有两台机器 xff0c 避免机器之间文件传来传去 xff0c 可以使用局域网文件共享 xff0c 在一台机器上开启文件共享 xff0c 另一台机器通过IP访问 xff0c 即可轻松实现文件互访 今天介绍我们
  • 模拟画图题P1185 绘制二叉树

    可能更好的观看体验 题目链接P1185 绘制二叉树 题意概述 根据规则绘制一棵被删去部分节点的满二叉树 节点用 o o o 表示 xff0c 树枝用 表示 每一层树枝长度会变化 xff0c 以满足叶子结点有如下特定 xff1a 相邻叶子节点
  • win7+MySQL5.7.18zip版本安装

    mysql5 7 18zip版本在windows的安装 xff0c 就是解压 xff0c 初始化 xff0c 然后做一些密码修改的设置即可使用 xff0c 如果需要远程连接 xff0c 需要更改用户表的host值为 39 39 xff0c
  • redhat7源码编译hadoop2.6.0

    以前在32位linux机器上编译过hadoop2 6 0 这次在redhat7 64bit上再次编译hadoop2 6 0 xff0c 除必须的jdk maven protobuf需要安装之外 xff0c 还需要安装系统依赖库gcc gcc
  • elasticsearch启动错误

    最近想尝试一下elk搭建实时日志分析系统 xff0c 结果运行elasticsearch时 xff0c 就遇到了一些问题 这些问题基本都是系统参数相关的 现在整理出来 xff0c 以免后面再次遇到 xff0c 也供大家参考 xff0c 少走
  • kafka+flume+hdfs实时日志流系统初探

    本次实验 xff0c 主要为了测试将kafka的消息通过flume接收并存入hdfs xff0c 如果之前搭建过hadoop flume kafka的 xff0c 这里会很快就会完成 xff0c 思路比较清晰 xff0c 主要配置在flum
  • 让Eclipse中spring的xml配置文件出现属性和类提示

    在spring配置文件中可以让配置bean的时候出现提示 xff0c 这里需要做一些设置 设置包括安装springide插件 spring beans version xsd文件引入 xff0c 增加xml编辑提示的字符 xff0c 默认只
  • win7查看端口占用的进程

    之前遇到一个问题 xff0c 系统上mysql启动了 xff0c 无法通过navicat客户端来连接 xff0c 这就很郁闷了 xff0c 最后定位到问题 xff0c 是我机器上还开启了一个开发php的应用程序phpwamp 它自带了一个m
  • cxf+spring实现webservice

    1 构建maven项目 xff0c 工程结构如下 xff1a 这里需要特别指出就是cxf core 3 1 12 jar类路径META INF cxf下有一个cxf xml的配置文件 xff0c 这个在applicationContext