一、   软件准备

Eclipse 4.2.1

Maven 2.2.1

Spring 3.2.6

CXF 3.0.2

软件下载和Eclipse 安装 maven插件等请參考其它文章。

二、   步骤

1.        新建webproject,利用maven管理。例如以下:

project名为test,完毕以后。项目结构例如以下图:

src/main/java 准备放 java 程序。

src/main/resources准备放各类资源文件。

2.        加入代码

1)        定义服务接口

package com.test;import javax.jws.WebService;@WebService
public interface HelloWorld {public String sayHello();
}

由于仅仅是一个webservice的实验程序,所以很easy。仅仅有一个服务方法: sayHello(),利用 @WebService注解来声明这是一个webservice的接口。

2)        实现服务类

package com.test;import javax.jws.WebService;@WebService
public class HelloWorldImpl implements HelloWorld{public String sayHello(){return "Hello world!";}
}

完毕java代码加入后的项目结构例如以下:

3.        加入Spring-CXF 配置

在项目 src/main/webapp/WEB-INF 文件夹下新建XML定义:cxf-servlet.xml例如以下:

<?

xml version="1.0" encoding="UTF-8"?

> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: beans --> <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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWorldImpl" address="/HelloWorld"/> </beans> <!-- END SNIPPET: beans -->

该定义文件利用spring和CXF的功能,公布一个ID为helloWorld,实现类为com.test.HelloWorldImpl,公布相对路径为 /HelloWorld(相应绝对文件夹为: http://host:port/{WebAPPName}/HelloWorld)的 webservice。

由于我们须要用到CXF来做webservice。右键点击项目中的POM.XML。加入apache-cxf依赖。例如以下图:

4.        Web应用配置

改动 src/main/webapp/WEB-INF 文件夹下的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>cxf</display-name><servlet><description>Apache 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>1</load-on-startup></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><session-config><session-timeout>60</session-timeout></session-config>
</web-app>

该文件实际上是定义了处理webservice的CXF Servlet的映射关系。

完毕步骤3和4以后的project文件夹例如以下:

5.        编译打包

利用maven(package -X)编译打包成test.war

(在Eclipse上右击project名 Run as -> Maven build)

6.        将步骤5生成的test.war部署到tomcatserver上

7.        訪问測试:

在浏览器上输入:http://localhost:8080/test/。出现例如以下画面就成功了:

点击WSDL链接:

8.        编写webservice client端代码

1)        首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src\main\resources 文件夹下创建client-beans.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder the License.
-->
<!-- START SNIPPET: beans -->
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schema/jaxws.xsd"><bean id="client" class="com.test.HelloWorld"factory-bean="clientFactory" factory-method="create"/><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.test.HelloWorld"/><property name="address" value="http://localhost:8080/test/HelloWorld"/></bean>
</beans>
<!-- END SNIPPET: beans -->

须要注意的是。该配置文件里的 address须要写成公布服务的绝对路径。

2)        编写clientjava代码: HelloWorldClient.java

package com.test;import org.springframework.context.support.ClassPathXmlApplicationContext;public final class HelloWorldClient {private HelloWorldClient() {}public static void main(String args[]) throws Exception {// START SNIPPET: clientClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});HelloWorld client = (HelloWorld)context.getBean("client");String response = client.sayHello();System.out.println("Response: " + response);System.exit(0);// END SNIPPET: client}
}

注意。代码中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client须要与"client-beans.xml"中的 bean id一致才干找到这个服务。

如今的项目结构例如以下:

3)        连接測试

在eclipse中直接按HelloWorldClient执行 Run as -> Java Application:

输出的Hello world! 即是我们公布的HelloWorld的方法 sayHello()的输出!这说明从服务公布到client连接都成功了。

转载于:https://www.cnblogs.com/blfbuaa/p/6961932.html

Eclipse+Maven+Spring+CXF 构建webservice 服务相关推荐

  1. Springboot 基于CXF构建WebService服务

    前言 最近因为系统需要接入了一个新的支付通道,一般来说都是使用RestApi 来接入,但是本次接入的支付通道为境外支付,使用的WebService,对于WS我们在实际业务中基本上不会用到,所以查阅了一 ...

  2. cxf开发webservice服务端怎么返回固定的报文格式_Spring boot webservice怎么玩? 第277篇...

    相关历史文章(阅读本文之前,您可能需要先看下之前的系列?) WebService SOAP概述 - 第275篇 WSDL是什么"Lese" - 第276篇 一.前言 当官不为民做主 ...

  3. java cxf服务端代码_【JAVA】 cxf 生成 webservice 服务端代码

    CXF Apache CXF = Celtix + XFire.CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding . ...

  4. Spring Cloud构建微服务

    2019独角兽企业重金招聘Python工程师标准>>> Spring Cloud构建微服务 博客分类: 微服务 spring boot 架构 首先了解下项目结构 请忽略config- ...

  5. Spring微服务实战第2章 使用Spring Boot构建微服务

    第2章 使用Spring Boot构建微服务 基于微服务的架构具有以下特点. 有约束的--微服务具有范围有限的单一职责集.微服务遵循UNIX的理念,即应用程序是服务的集合,每个服务只做一件事,并只做好 ...

  6. 使用Spring Boot构建微服务(文末福利)

    本文主要内容 学习微服务的关键特征 了解微服务是如何适应云架构的 将业务领域分解成一组微服务 使用Spring Boot实现简单的微服务 掌握基于微服务架构构建应用程序的视角 学习什么时候不应该使用微 ...

  7. CXF创建WebService服务配置说明

    使用CXF创建WebService服务 1.描述 使用CXF创建WebService服务样例,使用CXF创建的服务不需要安装Tomcat也可以启动. 2.开发环境 Eclipse开发工具,JDK1.7 ...

  8. Spring Cloud构建微服务架构:分布式服务跟踪(整合zipkin)【Dalston版】

    通过上一篇<分布式服务跟踪(整合logstash)>,我们虽然已经能够利用ELK平台提供的收集.存储.搜索等强大功能,对跟踪信息的管理和使用已经变得非常便利.但是,在ELK平台中的数据分析 ...

  9. Spring Cloud构建微服务架构:分布式服务跟踪(整合logstash)【Dalston版】

    通过之前的<入门示例>,我们已经为两个由SpringCloud构建的微服务项目 trace-1和 trace-2引入了Spring Cloud Sleuth的基础模块 spring-clo ...

最新文章

  1. 在虚拟机中的Nginx的安装
  2. JVM -XX: 参数介绍
  3. 【渝粤教育】国家开放大学2018年秋季 0267-21T摄影技术 参考试题
  4. 自底向上——知识图谱构建技术初探
  5. xadmin入门使用
  6. shell之旅--将目录下的文件重命名为md5码+后缀名
  7. 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
  8. 微信小程序踩坑- tabBar.list[3].selectedIconPath 大小超过 40kb
  9. 8.网页找不到服务器
  10. 路由器的网络连接模式(桥接模式和路由模式)
  11. 笔记本电脑无法连接自己家的网络
  12. 2023年全国最新交安安全员精选真题及答案6
  13. slice,splice,substring,split
  14. 开源项目推荐:运动控制速度前瞻算法(Look-Ahead),连续小线段高速插补算法
  15. 知乎不能改用户名吗_不知道不能随便改名字吗
  16. 列维飞行的幂律意味着什么
  17. 如何创造被动收入 | 我的知乎Live
  18. 【DGL教程】第4章 图数据集
  19. 男人无所谓忠诚,忠诚是因为背叛的筹码太低;女人无所谓正派,正派是因为受到的引诱不够...
  20. 宇宙尽头是铁岭,互联网尽头是贷款

热门文章

  1. 请使用webdav_介绍下phpdav的使用功能价值
  2. java单例设计模式双重_Java 设计模式 ——单例模式(饿汉,懒汉,双重锁,静态内部类)...
  3. jsf教程_JSF教程
  4. sql 触发器未触发_SQL触发器–综合指南
  5. 什么是SQL Server DATEDIFF()方法?
  6. java多线程 线程安全_Java中的线程安全
  7. component_春天@Component
  8. Python strftime()
  9. C ++中的初始化程序列表– std :: initializer_list
  10. [已解决] org.hibernate.HibernateException:没有活动事务,get无效