Following this example (https://codenotfound.com/spring-ws-example.html) I'm trying to create a SOAP
service with spring-boot 2.2.0.RELEASE
starting from an existing WSDL
.
The point is that in the output class there is a field that must be sent via MTOM
and I cannot get it to work properly. With the following excerpt from my code, a response is returned with the base64
within the field <data>
.
//Configuration bean class
@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, "/ServiceWS/*");
}
@Bean(name = "DeliveryService")
public Wsdl11Definition defaultWsdl11Definition() {
return new SimpleWsdl11Definition(new ClassPathResource("/wsdl/DeliveryService.wsdl"));
}
// other stuff
}
// Endpoint class
@Endpoint
public class DeliverDocumentEndpoint {
@PayloadRoot(namespace = "http://pb.com/service/ws/delivery", localPart = "DeliverDocument")
@ResponsePayload
public DeliverDocumentResponse deliverDocument(@RequestPayload DeliverDocument request) throws DeliveryFault {
// DO STUFF WITH MTOM
}
}
// Field int output bean, via code generation
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ComposedDocument", propOrder = {"data"})
public class ComposedDocument {
@XmlElement(required = true)
@XmlMimeType("application/octet-stream")
protected DataHandler data;
// getter and setter
}
looking on the web I found an example (https://github.com/spring-projects/spring-ws-samples/tree/master/mtom) and I added another @Configuration
bean to my solution,
@EnableWs
@Configuration
public class MtomServerConfiguration extends WsConfigurationSupport {
@Bean
@Override
public DefaultMethodEndpointAdapter defaultMethodEndpointAdapter() {
List<MethodArgumentResolver> argumentResolvers = new ArrayList<>();
argumentResolvers.add(methodProcessor());
List<MethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
returnValueHandlers.add(methodProcessor());
DefaultMethodEndpointAdapter adapter = new DefaultMethodEndpointAdapter();
adapter.setMethodArgumentResolvers(argumentResolvers);
adapter.setMethodReturnValueHandlers(returnValueHandlers);
return adapter;
}
@Bean
public MarshallingPayloadMethodProcessor methodProcessor() {
return new MarshallingPayloadMethodProcessor(marshaller());
}
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.pb.service.ws.model");
marshaller.setMtomEnabled(true);
return marshaller;
}
}
however with this approach I got this falut:
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring xml:lang="en">
No adapter for endpoint
[public com.pb.service.ws.delivery.DeliverDocumentResponse it.m2sc.ws.endpoint.DeliverDocumentEndpoint.deliverDocument(com.pb.service.ws.delivery.DeliverDocument)
throws com.pb.service.ws.delivery.DeliveryFault]:
Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?
</faultstring>
</SOAP-ENV:Fault>
How can I solve the problem ?
SOLVED: when generate-sources plugin was running, the sources generated was splitted in 3 different packages. I had to list them all to Jaxb2Marshaller like this:
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("com.pb.service.ws.common", "com.pb.service.ws.delivery", "com.pb.service.ws.model");
marshaller.setMtomEnabled(true);
return marshaller;
}