来自现有 wsdl 的带有 spring-boot 和 mtom 的 soap 服务
soap service with spring-boot & mtom from an existing wsdl
按照这个例子 (https://codenotfound.com/spring-ws-example.html) 我正在尝试从现有的 WSDL
.
开始使用 spring-boot 2.2.0.RELEASE
创建一个 SOAP
服务
重点是在输出 class 中有一个字段必须通过 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;
}
按照这个例子 (https://codenotfound.com/spring-ws-example.html) 我正在尝试从现有的 WSDL
.
spring-boot 2.2.0.RELEASE
创建一个 SOAP
服务
重点是在输出 class 中有一个字段必须通过 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;
}