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中的定义,感觉自己写也很容易,就是定义数据结构。其他的没看过例子,也不知道能定义什么。
其他的也没怎么探索,就上代码说说一些小细节吧。
上面wsdl的<wsdl:operation name="getCountry">,跟GetCountryRequest和GetCountryResponse有关系,就是getCountry,首字母小写,去掉request。如果定义的是GetCountry和GetCountryResponse(我就遇到了),会导致只有output没有input的定义。
localPart的内容需要跟服务的请求对应上,比如我改为localPart = "getCountry",如果会提示:No endpoint mapping found for [SaajSoapMessage {http://yournamespace/ws}getCountryRequest]。
binding 加上soapAction:
p.s.补充,项目折腾了axis2,有个大小写问题,按照某规范必须要大写,axis2调用的BeanInfo方法,底层会判断,第一个第二个字母不是连续大写的,会把第一个字母改为小写,折腾大半天,跟踪代码,实在是嵌套太多层不知怎么入手,最后改用cxf解决了问题,参考这篇。
完。
官方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集成shiro详细教程(小结)
- SpringBoot集成Redis实现缓存处理(Spring AOP技术)
- Spring Boot集成持久化Quartz定时任务管理和界面展示
- Spring Boot集成Swagger2项目实战
- spring-boot 集成 rabbitmq
- spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十一):集成 Shiro 框架
- SpringBoot集成MyBatis开启事务
- Spring boot 和Mybatis的集成
- 百度富文本编辑器Ueditor 集成springboot(不修改源码)
- Spring Boot 集成Shiro和CAS
- spring boot集成EhCache(七)
- Java SpringBoot集成RabbitMq实战和总结
- spring boot:集成缓存redis
- 在SpringBoot中集成Redis
- Swagger2 SpringMVC集成(非SpringBoot)
- springboot之集成mybatis mongo shiro druid redis jsp
- SpringBoot - 日志集成 Log4j2
- Spring Boot集成持久化Quartz定时任务管理和界面展示(转)
- 玩转Spring Boot 集成Dubbo