add xml declaration in soap ws springboot project

25 Views Asked by At

I would need the response of the provided service to have the xml declaration. i have searched but could not find a solution that i can integrate into my code.

Any suggestions are appreciated!

this is my code:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
      public ServletRegistrationBean<Servlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);

        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;
      }
    
}
@Endpoint
public class WebServiceEndpoint {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebServiceEndpoint.class);
        
        @PayloadRoot(namespace = "http://license", localPart = "getContentInfo")
        @ResponsePayload
        
        @ResponseStatus(code = HttpStatus.OK, reason = "OK")
        
        public JAXBElement <getContentInfoResponse> getRequest(@RequestPayload JAXBElement<getContentInfo> request) {

            ObjectFactory factory = new ObjectFactory();
            getContentInfoResponse response = factory.createGetContentInfoResponse();
            ResponseLicense responseLicense = factory.createResponseLicense();
            responseLicense.setPlayHoursRemained(23456);
            responseLicense.setPlaysRemained(-1);
            responseLicense.setResultCode(0);
            //responseLicense.setResultMessage("");
            response.setGetContentInfoReturn(responseLicense);
            
            JAXBElement<getContentInfoResponse> xml = createJaxbElement(response, getContentInfoResponse.class);
            
            return xml;
          }
    
          private JAXBElement <getContentInfoResponse > createJaxbElement(getContentInfoResponse response, Class<getContentInfoResponse> clazz) {
              
              JAXBElement<getContentInfoResponse> xml = new JAXBElement<getContentInfoResponse>(new QName(clazz.getSimpleName()), clazz, response);

              return xml;
          }
            
}

I get the response I need...

<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>

but I would like the response header to carry the xml declaration

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope ....
0

There are 0 best solutions below