用cxf開發一個WebService很簡單,只須要下面幾步:html
1.定義接口apache
public interface HelloService { String hello(); }
2.實現ide
public class HelloServiceImpl implements HelloService { @Override public String hello() { return "hi,my name is gyoung "; } }
3.用ServerFactoryBean生成服務spa
public static void main(String[] args) { HelloServiceImpl helloworldImpl = new HelloServiceImpl(); //cxf發佈服務的工廠bean ServerFactoryBean svrFactory = new ServerFactoryBean(); //設置服務類 svrFactory.setServiceClass(HelloService.class); //設置服務地址 svrFactory.setAddress("http://localhost:9001/Hello"); //設置服務bean svrFactory.setServiceBean(helloworldImpl); svrFactory.create(); }
這樣,一個簡單的HelloWorld服務便生成成功了。.net
可是,這樣生成的服務有一個問題,wsdl中的soapAction屬性是空的code
<wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="hello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="hello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="helloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
這一段<soap:operation soapAction="" style="document"/>,若是是.net生成的服務,soapAction是有值的 xml
<wsdl:binding name="WebService1Soap" type="tns:WebService1Soap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
查看了好久的源碼,才發現,設置cxf設置soapAction是在org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean類中htm
它會去循環遍歷serviceConfigurations,調用其getAction方法來獲取action的值。但初始的serviceConfigurations只有DefaultServiceConfiguration和SoapBindingServiceConfiguration,這兩個類都沒有實現其基類AbstractServiceConfiguration中的getAction方法。因此getAction返回值是空,因此wsdl中的soapAction值也會是空。找到問題就好辦了,咱們在serviceConfigurations中增長一個config,在AbstractServiceConfiguration的衆多子類中,我發現MethodNameSoapActionServiceConfiguration有繼承getAction方法,因此咱們只須要在生成服務的時候,增長一個MethodNameSoapActionServiceConfigurationblog
配置就好了。繼承
public static void main(String[] args) { HelloServiceImpl helloworldImpl = new HelloServiceImpl(); //cxf發佈服務的工廠bean ServerFactoryBean svrFactory = new ServerFactoryBean(); svrFactory.getServiceFactory().getConfigurations().add(new MethodNameSoapActionServiceConfiguration()); //設置服務類 svrFactory.setServiceClass(HelloService.class); //設置服務地址 svrFactory.setAddress("http://localhost:9001/Hello"); //設置服務bean svrFactory.setServiceBean(helloworldImpl); svrFactory.create(); }
最張生成的wsdl
<wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="hello"> <soap:operation soapAction="hello" style="document"/> <wsdl:input name="hello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="helloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
固然,咱們也能夠本身繼承AbstractServiceConfiguration來實現getAction方法。轉自:https://www.cnblogs.com/Gyoung/p/5469536.html