用 web方式发布 webService 服务端、客户端

一、服务器端搭建

1.首先创建 一个web工程(增加Maven依赖)

2.增加Maven依赖包,如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.jlm</groupId>
<artifactId>WebserviceTest</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>war</packaging> <dependencies>
<!-- spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.</version>
</dependency> <!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>2.5.</version>
</dependency> <!-- spring context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.5.</version>
</dependency> <!-- spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.5.</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency> <dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
<type>pom</type>
</dependency> <dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1</version>
</dependency> <dependency>
<groupId>xfire</groupId>
<artifactId>saaj-api</artifactId>
<version>1.3</version>
</dependency> <dependency>
<groupId>xfire</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3</version>
</dependency> <dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.2.</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.2.</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.2.</version>
</dependency>
</dependencies>
</project>

3. 编写HelloWorld 接口类 代码如下:

package net.cc.service;

import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface HelloWorld { String sayHello(@WebParam(name = "userName") String userName); }

  

说明:

@webService    说明这是一个webService

@webParam     说明参数名称

4. 编写实现类如下:

package net.cc.service;

import javax.jws.WebParam;
import javax.jws.WebService; @WebService(serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld { @Override
public String sayHello(@WebParam(name = "userName") String userName) {
// TODO Auto-generated method stub
System.out.println("客户端提交信息: " + userName);
return "say Hello " + userName;
}
}

说明:

@webService(serviceName = “HelloWorld”)    让Apache cxf知道是哪个接口来创建的WSDL

5. 编写spring 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://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"> <jaxws:endpoint id="ProjectManager" implementor="net.cc.service.HelloWorldImpl"
address="http://127.0.0.1:7890/HelloWorld" /> </beans>

说明:

implementor  表示 实现类 路径

address 表示需要发布的wsdl地址

6.编写 myListener 类 如下:

package net.cc.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.context.support.ClassPathXmlApplicationContext; public class myListener implements ServletContextListener { @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub } @Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("启动Tomcat...");
ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); } }

说明:

实现 ServletContextListener 目的是为了在Tomcat启动时自动加载

使用 ClassPathXmlApplicationContext 去加载刚才写的 spring-beans.xml 文件

7. 在当前项目中web.xml文件  增加如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>WebserviceTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <listener>
<listener-class>net.cc.servlet.myListener</listener-class>
</listener>
</web-app>

  

说明:

实现 ServletContextListener 接口的类路径

8 tomcat 启动截图:

9  访问web界面 截图:

二、客户端搭建

JDK提供的生成客户端的命令。

1、在cmd命令中输入:wsimport -s 指定代码生成目录 -p 包名 -keep webservice访问地址url

示例:wsimport -s E:\\AllWorkSpace\\MyWork\\TheClient\\src -p com.eastcom.ws.client -keep http://localhost:8080/Dom4j_AxisDemo/service/hello?wsdl

同样注意中间的空格!!!

目录地址中不能含有空格,发布地址不要忘了?wsdl

完成后如下图:

2、编写测试代码

package com.jlm.client;

public class Test {

	public static void main(String[] args) {

		HelloWorld hw = new HelloWorld_Service().getHelloWorldImplPort();
hw.sayHello("你好!!!"); }
}

 3、测试结果截图

maven搭建webservice apache cxf实现的更多相关文章

  1. Maven搭建webService (一) 创建服务端---使用main函数发布服务

    今天和大家分享下 使用maven 搭建 webService 服务端: 首先需要在你的IDE中集成Maven.集成办法此处略....... 1.创建一个web工程. 2.在pom文件中增加以下依赖: ...

  2. Maven搭建webService (三) 创建客户端---使用Apache CXF方式实现

    package test; import net.cc.web.server.HelloWorld; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean ...

  3. Maven搭建webService (二) 创建服务端---使用web方式发布服务

    今天和大家分享 使用 web方式发布 webService 服务端.客户端 1.首先创建 一个web工程(增加Maven依赖) 2.增加Maven依赖包,如下: <!-- spring core ...

  4. eclipse+maven搭建cxf webservice 完整例子

    开发环境是eclipse , maven. 在开发java webservice时,有两个比较流行的框架:axis2和cxf.cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在t ...

  5. 使用CXF和spring搭建webService服务

    虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验 ...

  6. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  7. Apache CXF实现WebService入门教程(附完整源码)

    Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...

  8. JAVAEE——BOS物流项目07:WebService入门、apache CXF入门、基于CXF发布CRM服务

    1 学习计划 1.WebService入门 n 什么是WebService n 调用网络上的WebService服务 n SOAP和WSDL概念 n 基于JDK1.7发布一个简单的WebService ...

  9. 分布式架构探索 - 2. WebService RPC框架之Apache CXF

    Apache CXF是一个开源的WebService RPC框架. 例子: 1. 新建一个maven web项目, 添加pom 如下: <?xml version="1.0" ...

随机推荐

  1. 大型情感剧集Selenium:8_selenium网页截图的四种方法

    有时候,有时候,你会相信一切有尽头-当你的代码走到了尽头,那么保留最后一刻的状态尤为重要,此时你该如何操作?记录日志-没有将浏览器当前的状态进行截图来的直观! 那么,selenium截取截屏,有哪些方 ...

  2. 华为ARM64服务器上手体验--不吹不黑,用实际应用来看看TaiShan鲲鹏的表现

    背景 中美贸易冲突以来,相信最大的感受,并不是我对你加多少关税,而是我有,可我不卖给你."禁售"成了市场经济中最大的竞争力. 相信也是因为这个原因,华为"备胎转正&quo ...

  3. virtualenv 在windows下的简单应用

    https://docs.python.org/zh-cn/3/tutorial/venv.html cmd下的操作: pip  install virtualenv pip install virt ...

  4. linux gre隧道创建

    目录 linux gre隧道创建 实验环境 实验目的 实验步骤 1.在host A(10.10.10.47)上面操作 2.在host B(192.168.0.118)上面操作 实验结果 还原实验环境 ...

  5. Codeforces Round #595 (Div. 3) D2Too Many Segments,线段树

    题意:给n个线段,每个线段会覆盖一些点,求删最少的线段,使得每个点覆盖的线段不超过k条. 思路:按右端点排序,之后依次加入每个线段,查询线段覆盖区间内的每个点,覆盖的最大线段数量,如果不超过k,那就可 ...

  6. hdu2546

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  7. 分布式监控数据采集系统Ganglia实战

    一.什么是Ganglia 对于这个工具,大家可能比较陌生,但是它功能非常强大,如果我们想收集所有服务器.网络设备的数据,那么ganglia绝对是首选,在深入学习之前,还是先从基础概念了解起吧! Gan ...

  8. R语言计算IV值

    更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 在对变量分箱后,需要计算变量的重要性,IV是评估变量区分度或重要性的统计量之一,R语言计算IV值的代码如下: Ca ...

  9. art-template模板判断

    1.添加模板 <script id="userinfo" type="text/template">           {{ if id == n ...

  10. 【Flutter】372- Flutter移动端实战手册

    ☝点击上方蓝字,关注我们! 本文字数:3705字 预计阅读时间:28分钟 导 读 Flutter又双叒叕来了!本周推送是我们Flutter系列文章的最终篇!<Flutter移动端实战手册> ...