SpringBoot SOAP WebService响应添加XML声明方案咨询
Spring Boot SOAP WebService 响应添加XML声明的解决方案
当前你的Spring Boot SOAP接口返回的响应缺少<?xml version="1.0" encoding="utf-8"?>声明,可通过自定义SOAP消息工厂配置解决,具体步骤如下:
步骤1:配置自定义SOAP消息工厂
在WebServiceConfig类中添加SaajSoapMessageFactory的Bean,开启XML声明输出:
@Bean public SaajSoapMessageFactory soapMessageFactory() { SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(); // 开启XML声明输出 messageFactory.setOutputXmlDeclaration(true); return messageFactory; }
步骤2:注入消息工厂到DispatcherServlet
修改messageDispatcherServlet的Bean定义,将上面的消息工厂注入进去:
@Bean public ServletRegistrationBean<Servlet> messageDispatcherServlet(ApplicationContext applicationContext, SaajSoapMessageFactory soapMessageFactory) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); // 设置自定义SOAP消息工厂 servlet.setMessageFactory(soapMessageFactory); return new ServletRegistrationBean<>(servlet, "/license/services/*"); }
完整配置代码
修改后的WebServiceConfig完整代码如下:
@EnableWs @Configuration public class WebServiceConfig extends WsConfigurerAdapter { @Bean public SaajSoapMessageFactory soapMessageFactory() { SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(); messageFactory.setOutputXmlDeclaration(true); return messageFactory; } @Bean public ServletRegistrationBean<Servlet> messageDispatcherServlet(ApplicationContext applicationContext, SaajSoapMessageFactory soapMessageFactory) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setMessageFactory(soapMessageFactory); return new ServletRegistrationBean<>(servlet, "/license/services/*"); } @Bean(name = "License") public Wsdl11Definition defaultWsdl11Definition() { SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition(); wsdl11Definition.setWsdl(new ClassPathResource("/wsdl/avs_2.wsdl")); return wsdl11Definition; } }
效果验证
修改完成后,接口响应会自动带上XML声明头:
<?xml version="1.0" encoding="utf-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/> <SOAP-ENV:Header/> <SOAP-ENV:Body> <getContentInfoResponse xmlns:ns2=http://license> <ns2:getContentInfoReturn> <ns2:playHoursRemained>23456</ns2:playHoursRemained> <ns2:playsRemained>-1</ns2:playsRemained> <ns2:properties xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:nil="true"/> <ns2:resultCode>0</ns2:resultCode> <ns2:resultMessage xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:nil="true"/> </ns2:getContentInfoReturn> </getContentInfoResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
内容的提问来源于stack exchange,提问作者Sergio Giannone
相关产品推荐
相关产品推荐

