WebService教程

article/2025/10/12 21:41:20

最近在学习WebService,今天尝试用Eclipse的插件生成JAX-WS WebService,结果遇到了不少的问题啊,调试了大半天终于把程序跑通了。现在把步骤和问题记录一下,也为了以后遇到相同的问题时能够及时解决。首先利用Eclipse生成WebService的服务端。
1、 创建一个web工程,DynamicWeb Project (File->New->Dynamic Web Project),取名叫“ws-server”
这里写图片描述
1、 编写提供服务的接口和实现类
AddService.java,注意两点:(1)在接口定义之前加上@WebService标注,表明这是一个WebService服务,否则在生成服务端时不能找到相应的接口;(2)这里@WebService标注的targetNamespace一定要填写内容,不然在生成WebService服务端的时候会报如下的错误,这个命名空间起始就是包名的倒序。

 IWAB0014E Unexpected exception occurred.  The name "" is not legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty Strings.  org.jdom.IllegalNameException: The name "" is not legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty Strings.  
 package ws.demo.service;  import javax.jws.WebService;  @WebService(targetNamespace="http://service.demo.ws/")  public interface AddService {  public int add(int a, int b);  }  

AddServiceImpl.java,同样的定义这个实现类之前也要加上@WebService标注,并且指向它实现的接口。

package ws.demo.service.impl;  import javax.jws.WebService;  import ws.demo.service.AddService;  @WebService(endpointInterface = "ws.demo.service.AddService")  public class AddServiceImpl implements AddService {  @Override  public int add(int a, int b) {  return a + b;  }  }  

写好提供服务的接口和实现类后的项目结构:
这里写图片描述
3、生成WebService服务(File->New->Other->Web Services->WebService),在Service implementation中选择提供服务的实现类。
这里写图片描述
可以在Configuration里面选择运行的环境,我使用的是Tomcat 7.0和CXF,在选择CXF作为运行环境的时候需要先将CXF的环境导入进行Eclipse,否则会报Unable to add the follwing facets to project ws-server: CXF 2.x Web Services错误。导入过程如下:
这里写图片描述
4、以上配置完成后,生成WebService服务(File->New->Other->WebServices->Web Service),选择提供服务的实现类,配置运行环境,然后点击Next,选择或生成一个提供服务的接口,选择完成后,点击Next。
这里写图片描述
5、 选择需要加标注的内容,包括@WebMethod, @WebParam, @RequestWrapper, @ResponseWrapper, @WebResult等等,选择完成后,点击Next。
这里写图片描述
6、选择Java2WS的配置信息,同时也可以选择SOAP的版本,然后点击Next,Eclipse会在后台生成相应的文件并在控制台打印日志。
这里写图片描述

 java2ws -cp E:\workspace\ws-server\build\classes -s E:\workspace\ws-server\.cxftmp/src -d E:\workspace\ws-server\.cxftmp/wsdl -classdir E:\workspace\ws-server\build\classes -o addserviceimpl.wsdl -createxsdimports -verbose -frontend jaxws -databinding jaxb -wsdl -wrapperbean ws.demo.service.impl.AddServiceImpl  java2ws - Apache CXF 3.1.6  六月 14, 2016 3:51:03 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass  信息: Creating Service {http://impl.service.demo.ws/}AddServiceImplService from class ws.demo.service.AddService  

生成后的项目结构:
这里写图片描述
生成服务端时要将Eclipse的jre改成jdk包中的jre,否则会报:

 Exception in thread "main" java.lang.NullPointerException  at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:190)  at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:144)  at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:139)  

同时要将Tomcat会读取默认的jre,这里也需要将Tomcat的jre改掉。修改完成后的jre版本:
这里写图片描述

7、 按理说现在需要的文件都已经生成了,程序应该可以运行了,但是实际情况并非如此,先启动服务器,结果报错:

 严重: Context initialization failed  org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxf-extension-soap.xml]  Offending resource: ServletContext resource [/WEB-INF/cxf-beans.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist  at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)  

这是因为WEB-INF目录下的cxf-beans.xml文件里有

 <import resource="classpath:META-INF/cxf/cxf.xml" />  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  

这几段将cxf.xml、cxf-extension-soap.xml、cxf-servlet.xml引进来了,但是我使用的CXF3.1.6版本并不会生成这几个文件,所以需要将这几段删除。同时还要删除cxf-services-ws-discovery-api-3.1.4.jar、cxf-services-ws-discovery-service-3.1.4.jar、cxf-services-wsn-api-3.1.4.jar、cxf-services-wsn-core-3.1.4.jar这几个jar包,否则在启动后虽然服务可以发布,但是过一段时间就会报prefix wsdp is not bound to a namespace。
现在是否就可以运行了呢?先运行以下服务器,结果又报错:

  严重: Context initialization failed  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addservice': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Could not find definition for service {http://service.demo.ws/}AddServiceImplService.  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)  

这是因为在命名空间http://service.demo.ws/下找不到AddServiceImplService,观察项目结构发现根本就没有叫这个名字的类,那这个名字是从哪里来的呢?再看cxf-beans.xml,里面有一段:

 <jaxws:endpoint xmlns:tns="http://service.demo.ws/" id="addservice"  implementor="ws.demo.service.impl.AddServiceImpl" wsdlLocation="wsdl/addserviceimpl.wsdl"  endpointName="tns:AddServiceImplPort" serviceName="tns:AddServiceImplService"  address="/AddServiceImplPort">  <jaxws:features>  <span style="white-space:pre">    </span><bean class="org.apache.cxf.feature.LoggingFeature" />  </jaxws:features>  </jaxws:endpoint>  

可以看到,这里的jaxws:endpoint属性里面有serviceName的属性,属性名为AddServiceImplService,这些都是框架自动生成文件时加上的,现在把这些不需要的删去,改为如下的内容:

 <jaxws:endpoint xmlns:tns="http://service.demo.ws/" id="addservice"  implementor="ws.demo.service.impl.AddServiceImpl" wsdlLocation="wsdl/addserviceimpl.wsdl">  <jaxws:features>  <bean class="org.apache.cxf.feature.LoggingFeature" />  </jaxws:features>  </jaxws:endpoint>  

再启动服务器,启动成功,访问http://localhost:8080/ws-server/services/,出现如下结果:
这里写图片描述
再点击wsdl的链接,可以看到生成的wsdl文件:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.demo.ws/"  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://service.demo.ws/"  name="AddServiceImplService" targetNamespace="http://impl.service.demo.ws/">  <wsdl:import  location="http://localhost:8081/ws-server/services/AddServiceImplPort?wsdl=AddService.wsdl"  namespace="http://service.demo.ws/"></wsdl:import>  <wsdl:binding name="AddServiceImplServiceSoapBinding"  type="ns1:AddService">  <soap:binding style="document"  transport="http://schemas.xmlsoap.org/soap/http" />  <wsdl:operation name="add">  <soap:operation soapAction="" style="document" />  <wsdl:input name="add">  <soap:body use="literal" />  </wsdl:input>  <wsdl:output name="addResponse">  <soap:body use="literal" />  </wsdl:output>  </wsdl:operation>  </wsdl:binding>  <wsdl:service name="AddServiceImplService">  <wsdl:port binding="tns:AddServiceImplServiceSoapBinding"  name="AddServiceImplPort">  <soap:address  location="http://localhost:8081/ws-server/services/AddServiceImplPort" />  </wsdl:port>  </wsdl:service>  </wsdl:definitions>  

服务端编写成功。


http://chatgpt.dhexx.cn/article/6Y8tAtlr.shtml

相关文章

WebService入门教程(服务端发布WebService)

本篇内容过多&#xff0c;时间紧迫的朋友可以通过目录快速筛选自己想要看的内容&#xff0c;本人接触webservice也没多久&#xff0c;也处于学习阶段&#xff0c;如果有错误请指正&#xff0c;如果已经是大神请略过这篇文章&#xff0c;这篇文章不涉及webservice的底层原理&…

WebService最详细教程

WebService相关概念 基础概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,就是说服务端程序采用java编写&#xff0c;客户端程序则可以采用其他编程语言编写&#xff0c;反之亦然&#xff01;跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统…

webSerivce简明使用教程

茫茫人海千千万万&#xff0c;感谢这一秒你看到这里。希望我的文章对你的有所帮助&#xff01; 愿你在未来的日子&#xff0c;保持热爱&#xff0c;奔赴山海&#xff01; &#x1f440; 前言 因为前段时间&#xff0c;需要使用到webService来调用公司的其他系统api接口&#x…

WebService详细讲解

1 学习目标2 webservice 基本概念 2.1 什么是web服务2.2 简介2.3 术语 2.3.1 webservice开发规范2.3.2 SOAP 协议2.3.3 wsdl说明书2.3.4 UDDI2.4 应用场景2.5 优缺点 2.5.1 优点&#xff1a;2.5.2 缺点&#xff1a;2.6 面向服务架构SOA3 ApacheCXF 框架介绍 3.1…

11步教你入门webservice

新建立一个javaWeb项目&#xff0c;一个java类&#xff0c;如图&#xff1a; 1.具体很简单 首先要创建一个web工程 2、接下来我们就要将项目中的TestService的这个类生成WebService服务端&#xff0c;选择new Web Service&#xff0c;如图&#xff1a; 3 4.Next,选择jav…

php 实现分页功能(class封装了功能)

前言 分页是一个很常见的功能&#xff0c;我这里提供了分类类(class)&#xff0c;用于前端页面中的四个按钮&#xff1a; 首页下一页上一页尾页 上面的演示非常不直观&#xff0c;但足可以证明这个类可以完成分页功能。 完整的代码 附有非常详细的注释&#xff0c;但需要有…

PHP分页技术详解

直接上代码&#xff0c;注释很详细了。 <?php /** * php分页技术详解 * author jahng */header("Content-type: text/html; charsetUTF-8");echo<link rel"stylesheet" type"text/css" href"css.css" media"all" /&g…

PHP的分页处理

HTML页面&#xff1a; <div class"page"> {$data->render()|raw} </div> 控制器页面&#xff1a; $data ArticleInfo:: where(cate_id, $this->cid)->whereLike(title,%.$get.%)->paginate([ query>[cid>$this->cid], l…

php分页

这是学生信息管理每三条一页> <?phpfunction news($pageNum 1, $pageSize 3){$array array();$mysqlnew mysql();$rs "select * from user limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;$r $mysql->query($rs);while ($obj…

PHP-分页

1.6 分页 1.6.1 分析 -- 1、获取当前页码的数据 页码 SQL语句 1 select * from products limit 0,10 2 select * from products limit 10,10 3 select * from products limit 20,10 结论&#xff1a; $pageno&#xff1a;页码 $startno:起始位置 $pagesize10:页面大小…

PHP分页查询

1、创建Page.php类&#xff0c;代码如下&#xff1a; <?php /*** 分页模板类*/ class Page {private $cur_page;//当前页private $total;//总条数private $page_size 10;//每页显示的条数private $total_page;//总页数private $first_page;//首页显示名称private $pre_pa…

全功能PHP分页条

网上可以找到的ASP、PHP分页条很多。我也不能免俗&#xff0c;发表一个献献丑。唯一聊以自慰的是这个分页条能生成的显示样式还是很多的&#xff0c;相信能满足大部分人的需要。另一个特点就是使用特别简单&#xff0c;一般传递两个参数即可使用。文档里有使用样例和效果图。发…

php原生分页

自己写一个原生php分页&#xff1a; <?php $linkMySQL_connect(localhost,用户名,密码); mysql_select_db(数据库名称,$link); mysql_query(set names utf8); $resultmysql_query("select * from 表名"); $count mysql_num_rows($result); $Page_size10; …

如何用php实现分页效果

分页效果在网页中是常见的,可是怎样才能实现分页呢,今天做了两种方法来实现一下分页的效果 首先,我们需要准备在数据库里面准备一个表,并且插入数据,这些都是必需的前提工作了,不多说,如图所示(库名为jereh,表名为n_content): 步骤分析: 我们需要分页的话,需要用…

超级好用的PHP分页类

<?phpclass Page {private $total; //总记录private $pagesize; //每页显示多少条private $limit; //limitprivate $page; //当前页码private $pagenum; //总页码private $url; //地址private $bothnum; //两边保持数字分页的量//构造方法初始化pub…

PHP-分页具体实现及代码

数据分页概述 对大量数据进行分页显示是 Web 开发中最常见的情况&#xff0c;但大多刚开始接触 Web 开发的开发人员&#xff0c;对分页技术往往比较迷惘&#xff0c;本节教程以一个分页显示留言板的数据为例就来演示一下 PHP 中基本的数据分页显示原理。 本节教程需要用到的 …

计算机网络原理复习(一)

最近面试某公司的Linux C开发职位&#xff0c;面试的时候面试官提问了一些网络基础的知识&#xff0c;只是惭愧至极&#xff0c;好多知识点已经记忆模糊。周末花时间把网络原理的知识点整理了一下。 计算机网络体系结构&#xff1a; 网络体系结构&#xff1a; 按照我的理解 1…

计算机网络原理练习题及答案

计算机网络原理练习题及答案 读前必看&#xff0c;此篇为计算机网络原理复习对应的练习题。 第一部分传送门&#xff1a;待更新 文章目录 计算机网络原理练习题及答案练习1练习2练习3练习 4练习 5练习 6 练习1 某单位申请到一个B类IP地址&#xff0c;其网络标识&#xff08;N…

计算机网络原理 谢希仁(第8版)第一章习题答案

1-01 计算机网络可以向用户提供哪些服务&#xff1f; 1-02 试简述分组交换的要点。 答&#xff1a;采用了存储转发技术&#xff0c;即将报文&#xff08;要发送的整块数据&#xff09;划分为几个较小的等长数据段&#xff0c;在每个数据段前加上必要的控制信息组成的首部&#…

计算机网络原理(谢希仁第八版)第五章课后习题答案

第五章 35题&#xff0c;36题已经做了更正&#xff0c;特别感谢粉丝奈七七的答案。 1.试说明运输层在协议栈中的地位和作用&#xff0c;运输层的通信和网络层的通信有什么重要区别&#xff1f;为什么运输层是必不可少的&#xff1f; 答&#xff1a;运输层处于面向通信部分的最…