按照此示例(https://codenotfound.com/spring-ws-example.html),我正在尝试创建一个SOAP
服务,spring-boot 2.2.0.RELEASE
从现有WSDL
开始。
关键是在输出类中有一个字段必须通过MTOM
发送,我无法让它正常工作。使用我的代码中的以下摘录,返回一个响应,其中包含字段<data>
中的base64
。
//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
}
在网上查找,我找到了一个示例(https://github.com/spring-projects/spring-ws-samples/tree/master/mtom),并在解决方案中添加了另一个@Configuration
bean,
@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;
}
}
但是通过这种方法,我得到了这个法鲁特:
<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>
我该如何解决问题?
已解决:当生成源插件运行时,生成的源被拆分为 3 个不同的包。我不得不像这样将它们全部列出给Jaxb2Marshaller:
@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;
}