用cxf开发一个WebService很简单,只需要下面几步:

1.定义接口

  1. public interface HelloService {
  2. String hello();
  3. }

2.实现

  1. public class HelloServiceImpl implements HelloService {
  2. @Override
  3. public String hello() {
  4. return "hi,my name is gyoung ";
  5. }
  6. }

3.用ServerFactoryBean生成服务

  1. public static void main(String[] args) {
  2. HelloServiceImpl helloworldImpl = new HelloServiceImpl();
  3. //cxf发布服务的工厂bean
  4. ServerFactoryBean svrFactory = new ServerFactoryBean();
  5. //设置服务类
  6. svrFactory.setServiceClass(HelloService.class);
  7. //设置服务地址
  8. svrFactory.setAddress("http://localhost:9001/Hello");
  9. //设置服务bean
  10. svrFactory.setServiceBean(helloworldImpl);
  11. svrFactory.create();
  12. }

这样,一个简单的HelloWorld服务便生成成功了。

但是,这样生成的服务有一个问题,wsdl中的soapAction属性是空的

  1. <wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloServicePortType">
  2. <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  3. <wsdl:operation name="hello">
  4. <soap:operation soapAction="" style="document"/>
  5. <wsdl:input name="hello">
  6. <soap:body use="literal"/>
  7. </wsdl:input>
  8. <wsdl:output name="helloResponse">
  9. <soap:body use="literal"/>
  10. </wsdl:output>
  11. </wsdl:operation>
  12. </wsdl:binding>

这一段<soap:operation soapAction="" style="document"/>,如果是.net生成的服务,soapAction是有值的

  1. <wsdl:binding name="WebService1Soap" type="tns:WebService1Soap">
  2. <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
  3. <wsdl:operation name="HelloWorld">
  4. <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document"/>
  5. <wsdl:input>
  6. <soap:body use="literal"/>
  7. </wsdl:input>
  8. <wsdl:output>
  9. <soap:body use="literal"/>
  10. </wsdl:output>
  11. </wsdl:operation>
  12. </wsdl:binding>

查看了很久的源码,才发现,设置cxf设置soapAction是在org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean类中

它会去循环遍历serviceConfigurations,调用其getAction方法来获取action的值。但初始的serviceConfigurations只有DefaultServiceConfiguration和SoapBindingServiceConfiguration,这两个类都没有实现其基类AbstractServiceConfiguration中的getAction方法。所以getAction返回值是空,所以wsdl中的soapAction值也会是空。找到问题就好办了,我们在serviceConfigurations中增加一个config,在AbstractServiceConfiguration的众多子类中,我发现MethodNameSoapActionServiceConfiguration有继承getAction方法,所以我们只需要在生成服务的时候,增加一个MethodNameSoapActionServiceConfiguration

配置就行了。

  1. public static void main(String[] args) {
  2. HelloServiceImpl helloworldImpl = new HelloServiceImpl();
  3. //cxf发布服务的工厂bean
  4. ServerFactoryBean svrFactory = new ServerFactoryBean();
  5. svrFactory.getServiceFactory().getConfigurations().add(new MethodNameSoapActionServiceConfiguration());
  6. //设置服务类
  7. svrFactory.setServiceClass(HelloService.class);
  8. //设置服务地址
  9. svrFactory.setAddress("http://localhost:9001/Hello");
  10. //设置服务bean
  11. svrFactory.setServiceBean(helloworldImpl);
  12. svrFactory.create();
  13. }

最张生成的wsdl

  1. <wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloServicePortType">
  2. <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  3. <wsdl:operation name="hello">
  4. <soap:operation soapAction="hello" style="document"/>
  5. <wsdl:input name="hello">
  6. <soap:body use="literal"/>
  7. </wsdl:input>
  8. <wsdl:output name="helloResponse">
  9. <soap:body use="literal"/>
  10. </wsdl:output>
  11. </wsdl:operation>
  12. </wsdl:binding>

当然,我们也可以自己继承AbstractServiceConfiguration来实现getAction方法。

cxf WebService设置wsdl中soapAction的值的更多相关文章

  1. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  2. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  3. cxf webservice 生成wsdl方法参数名称为arg0问题

    在通过cxf生成webservice服务时,如果你是用ServerFactoryBean,那么在生成wsdl时,方法的参数名称会被自动命名为arg0,arg1...,如: <xsd:comple ...

  4. webservice 从客户端中检测到有潜在危险的 Request.Form 值

    webservice中传递xml格式的参数时报错 webservice 从客户端中检测到有潜在危险的 Request.Form 值解决方案: 1.在web.config的system.web节点中添加 ...

  5. jQuery中设置form表单中action值与js有什么不同。。。。

    jQuery中设置form表单中action值与js有什么不同.... HTML代码如下: <form action="" method="post" i ...

  6. yii2中textarea中的默认值设置

    1. view中显示文本域的位置 <?= $form->field($goods_model, 'goods_introduce')->textArea(['class'=>' ...

  7. asp.net EF model中的默认值设置

    在做数据库规划时,通常会规划一些系统字段,也就是由数据库本身自行指定默认值到这个字段上,创建新的“创建时间(CreateDate)”字段就会常常这样设计. 如果希望能有默认值,且让.net 程序在新增 ...

  8. Jquery Ajax 异步设置Table中某列的值

    可根据table中某列中的ID去改变某列的值! 只是参考,实际应用中不能这样做的,如果有很多行,频繁访问服务器,服务器是顶不住的! JS: $(document).ready(function () ...

  9. jQuery中设置form表单中action值的方法

    jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈   html代码: <form id="myFormId&quo ...

随机推荐

  1. VB6史无前例的子类化之透明按钮

    [原创文章,转发请保留版权信息] 作者:mezstd 文章地址:http://www.cnblogs.com/imez/p/3299728.html 效果图: 请原谅笔者无耻地称之为史无前例,至少在笔 ...

  2. XMind怎么使用查找功能

    XMind思维导图中,XMind搜索功能与XMind查找替换功能乍一看有些相似,然而不尽相同,本文为你着重讲解XMind搜索功能. 首先在XMind思维导图中的工具栏找到"Search&qu ...

  3. Java相关书籍分享

    Java核心技术(卷1):基础知识(原书第9版) [Core Java Volume I-Fundamentals (Ninth Edition)].pdf Java核心技术(卷2):高级特性(原书第 ...

  4. div+css:div中图片垂直居中

    div中图片垂直居中 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  5. JUC学习笔记--Thread多线程基础

    实现多线程的两种方法 java 实现多线程通过两种方式1.继承Thread类 ,2.实现Runnable接口 class Newthead extends Thread{ public void ru ...

  6. JavaScript备忘录

    提取字符串substring(start,end)substr(start [, length ])JavaScript substr() 方法 --

  7. Theano Inplace

    Theano Inplace inplace Computation computation that destroy their inputs as a side-effect. Example i ...

  8. 面对bug和困难的心态

    遇到bug了? 作为程序员,会面对各种各样的bug,我们在编写代码的时候,也是生产bug的过程.在公司总会遇到老同事留下的代码,这些代码出现问题了该怎么办?最常见的想法就是, 老同事怎么考虑这么不周到 ...

  9. java 反射

    com.my.Ob; @Table(name="ob") class Ob{ @Id private Integer id; @Column(name="name1&qu ...

  10. linux中rz中的-e选项

    linux shell rz和sz是终端下常用的文件传输命令,rz和sz通过shell被调用,其中rz用于从启用终端的系统上传文件到目标系统(终端登录的目标系统), 这里不过多介绍这些命令,只是记录一 ...