您的位置:首页 > 编程语言 > Java开发

spring boot 集成spring-ws小结

2017-12-24 00:00 323 查看
因为需要用到webservice接收数据,本身项目用的就是spring boot,所以直接找到spring ws。

官方demo https://spring.io/guides/gs/producing-web-service/,也可以看看这个服务端客户端,差不多,主要就是需要编写xsd文件,参考demo中的定义,感觉自己写也很容易,就是定义数据结构。其他的没看过例子,也不知道能定义什么。

其他的也没怎么探索,就上代码说说一些小细节吧。

<xs:element name="getCountryRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="getCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="tns:country"/>
</xs:sequence>
</xs:complexType>
</xs:element>

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
GetCountryResponse response = new GetCountryResponse();
Country poland = new Country();
poland.setName("Poland-" + request.getName());
poland.setCapital("Warsaw");
poland.setCurrency(Currency.PLN);
poland.setPopulation(38186860);
response.setCountry(poland);
return response;
}

@Bean(name = "countries")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("CountriesPort");
wsdl11Definition.setLocationUri("/ws/countries.wsdl");
wsdl11Definition.setTargetNamespace("http://yournamespace/ws");
wsdl11Definition.setSchema(countriesSchema);
wsdl11Definition.setServiceName(""CountriesService");
return wsdl11Definition;
}

<wsdl:message name="getCountryRequest">
<wsdl:part element="tns:getCountryRequest" name="getCountryRequest"/>
</wsdl:message>
<wsdl:message name="getCountryResponse">
<wsdl:part element="tns:getCountryResponse" name="getCountryResponse"/>
</wsdl:message>
<wsdl:portType name="CountriesPort">
<wsdl:operation name="getCountry">
<wsdl:input message="tns:getCountryRequest" name="getCountryRequest"/>
<wsdl:output message="tns:getCountryResponse" name="getCountryResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCountry">
<soap:operation soapAction=""/>
<wsdl:input name="getCountryRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getCountryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

上面wsdl的<wsdl:operation name="getCountry">,跟GetCountryRequest和GetCountryResponse有关系,就是getCountry,首字母小写,去掉request。如果定义的是GetCountry和GetCountryResponse(我就遇到了),会导致只有output没有input的定义。

<wsdl:message name="getCountryResponse">
<wsdl:part element="tns:getCountryResponse" name="getCountryResponse"/>
</wsdl:message>
<wsdl:portType name="CountriesPort">
<wsdl:operation name="getCountry">
<wsdl:output message="tns:getCountryResponse" name="getCountryResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCountry">
<soap:operation soapAction=""/>
<wsdl:output name="getCountryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

localPart的内容需要跟服务的请求对应上,比如我改为localPart = "getCountry",如果会提示:No endpoint mapping found for [SaajSoapMessage {http://yournamespace/ws}getCountryRequest]。

binding 加上soapAction:

Properties prop = new Properties();
prop.setProperty("getCountry", "http://sap.com/xi/WebService/soap1.1");
wsdl11Definition.setSoapActions(prop);

<wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCountry">
<soap:operation soapAction="http://sap.com/xi/WebService/soap1.1"/>
<wsdl:input name="getCountryRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getCountryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

p.s.补充,项目折腾了axis2,有个大小写问题,按照某规范必须要大写,axis2调用的BeanInfo方法,底层会判断,第一个第二个字母不是连续大写的,会把第一个字母改为小写,折腾大半天,跟踪代码,实在是嵌套太多层不知怎么入手,最后改用cxf解决了问题,参考这篇

完。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Boot Spring-ws