我正在尝试在 Spring Boot 中设置 SOAP 服务,但是当我尝试访问 WSDL 文件(例如,http://localhost:8082/ws/AccountService.wsdl)时,我收到 404(找不到页面) )错误:
我有以下配置:
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext context) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(context);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean(name = "account")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema accountSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("AccountPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://example.com/accounts");
wsdl11Definition.setSchema(accountSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema accountSchema() {
return new SimpleXsdSchema(new ClassPathResource("wsdl/AccountService.wsdl"));
}
}
@Endpoint
public class AccountEndpoint {
private static final String NAMESPACE_URI = "http://example.com/accounts";
private final AccountSoapService accountSoapService;
public AccountEndpoint(AccountSoapService accountSoapService) {
this.accountSoapService = accountSoapService;
}
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetAccountRequest")
@ResponsePayload
public GetAccountResponse getAccount(@RequestPayload GetAccountRequest request) {
// Chiamata al servizio per ottenere la risposta in base all'accountId
return accountSoapService.getAccount(request.getAccountId());
}
}
发现我的配置错误
此配置有 2 个问题,调用有 1 个问题。
DefaultWsdl11Definition
的地方使用了
SimpleWsdl11Definition
<bean-name>.wsdl
一起使用,并且您的 Bean 被命名为 account
而不是 AccountService
。您可以通过删除整个
WebServiceConfig
并将以下内容添加到您的 application.properties
来修复基本上所有问题。
spring.webservices.path=/ws
spring.webservices.wsdl-locations=classpath:/wsdl/
这将使 Spring Boot 自动配置 Spring Web Services 并使用正确的名称公开 WSDL。