最近在学习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>  

服务端编写成功。

WebService教程相关推荐

  1. 最详细的idea创建webservice教程

    最详细的idea创建webservice教程 创建服务端 File->New Project 点击next,会自动生成demo 将要发布的类加上@WebService,方法加上@WebMetho ...

  2. WebService教程详解

    WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求,接下来通过本文给大家介绍WebService教程详解,对webservice教程感兴趣的 ...

  3. 孔浩老师 java 微信_孔浩老师JAVA WebService教程

    课程目录:01_webservice快速实例 02_wsimport的使用 03_wsdl和soap讲解(介入了tcpmon工具) 04_SOA的分析(纯属扯淡) 05_dtd讲解 06_schema ...

  4. Java小白翻身 - webservice教程2

    来一个HelloWorld,SpringBoot发布WebService可简单啦. 1.搭建项目 2.配置pom.xml 3.建services服务包 4.登陆接口类 5.登陆接口实现类 6.创建CX ...

  5. 史上最详cxf-Springmvc-maven实现webservice教程

    虽知道webservice,工作两年一直没使用过,最近不忙趁机研究了下,实现了简单的服务端及客户端调用.鉴于慕课网没有webservice的教程,大多又都是学生,就在这里跟大家分享下,内容比较详细.大 ...

  6. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  7. CXF WebService 教程

    业务需求:常见WEB服务: 手机淘宝.京东-. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的"进销存系统"在某商品缺货时自动给供应 ...

  8. webservice 教程学习系列(七)——编写天气预报和手机号码归属地的webservice

    1.编写天气预报的webservice 首先我们在http://www.webxml.com.cn/zh_cn/index.aspx里面找到天气预报的url 然后找到获取地区天气的接口方法: 看到方法 ...

  9. WebService教程实例

    一.准备工作 1.Myeclipse 2014 2.jdk8.0 二.创建服务端 1.创建[Web Service Project],命名为[TheService]. 2.创建[Class]类,命名为 ...

最新文章

  1. strchr strstr函数php,PHP字符串函数之 strstr stristr strchr strrchr
  2. KEGG在线数据库使用攻略
  3. 深入理解iframe
  4. 【正一专栏】卡卡退役-送别了多少人的青春和初恋
  5. linux用avk怎么提取字符,在Linux下进行视频音频格式转换提取等
  6. android8.1启动前台服务,Android - 保活(1)前台服务保活
  7. 两种驱动系统运行的方式--分时的方式
  8. uiautomator2进阶
  9. jsp mysql 插入数据_jsp连接MySQL实现插入insert操作功能示例
  10. Java Calendar hashCode()方法与示例
  11. 【Linux】安装x11vnc和xrdp,使用windows远程deepin
  12. #控制台大学课堂点名问题_草率了!大学课堂点名新招数来袭,逃课的一个也没有躲过...
  13. 震撼上市!北朝鲜语对话语音识别数据库
  14. Java新职篇:for循环
  15. win10忘记密码_win10带有密码压缩包的破解方法
  16. SPSS23第二版课后习题答案_全新版大学进阶英语综合教程3 Unit1unit3课后习题答案...
  17. FFmpeg合并ts文件为mp4文件
  18. 12个顶级思维模型,非常值得一看!
  19. 【无标题】互联网广告投放优势和前景
  20. 工具分享:ideaIU-2019.2.4_windows正版最新(附下载链接)

热门文章

  1. 如何实现对数ln运算?
  2. 在使用python-openpyxl包时遇到的坑之append方法
  3. 培养孩子成就“万能”家长
  4. Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行...
  5. 奥德赛海洋勘探公司(NASDAQ:OMEX)诉墨西哥案中的重要仲裁事项需要股东提供意见
  6. 吉利集团发展史成本演化(一天的成果)
  7. 代码管理工具GIT之图形界面工具TortoiseGit
  8. CAD垂直标注出现拐角的问题
  9. 一招先吃遍天,样样精通,样样稀松。
  10. 聚合支付需要什么证?