使用soap web服务在spring boot中加载多个bean
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用soap web服务在spring boot中加载多个bean相关的知识,希望对你有一定的参考价值。
我正在尝试使用spring boot编写soap webservice。我有2个xsd。 REL-6-MM7-1-4.xsd,它依赖于SoapEnvelope.xsd。
任何人都可以帮我解决它。
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
import org.springframework.xml.xsd.XsdSchemaCollection;
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
@Bean(name = "REL-6-MM7-1-4")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchemaCollection xsdSchemaCollection) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("MMSPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4");
wsdl11Definition.setSchemaCollection(xsdSchemaCollection);
return wsdl11Definition;
}
@Bean
public XsdSchemaCollection getXsdCollection() {
return new XsdSchemaCollection() {
public XsdSchema[] getXsdSchemas() {
return new XsdSchema[]{new SimpleXsdSchema(new ClassPathResource("REL-6-MM7-1-4.xsd")), new SimpleXsdSchema(new ClassPathResource("SoapEnvelope.xsd"))};
}
public XmlValidator createValidator() {
throw new UnsupportedOperationException();
}
};
}
我试图暴露2豆,但当我启动应用程序时,我得到以下错误。
在类路径资源[hello / WebServiceConfig.class]中定义名为'REL-6-MM7-1-4'的bean时出错:init方法的调用失败;嵌套异常是java.lang.NullPointerException
答案
尝试修改您的方法,如下所示它应该工作。
@Bean
public XsdSchema studentSchema() {
return new SimpleXsdSchema(new ClassPathResource("school.xsd"));
}
@Bean
public XsdSchema employeeSchema() {
return new SimpleXsdSchema(new ClassPathResource("employee.xsd"));
}
@Bean
public XsdSchemaCollection getXsdCollection() {
return new XsdSchemaCollection() {
@Override
public XmlValidator createValidator() {
// TODO Auto-generated method stub
return null;
}
@Override
public XsdSchema[] getXsdSchemas() {
return new XsdSchema[]{studentSchema(),employeeSchema()};
}};
另一答案
这是我做的:
wsdl11Definition.setSchemaCollection(updateXsds());
....
@Bean
public XsdSchemaCollection updateXsds() throws Exception {
CommonsXsdSchemaCollection xsds = new CommonsXsdSchemaCollection(
new ClassPathResource("xsd1.xsd"),
new ClassPathResource("xsd2.xsd")
);
xsds.setUriResolver(new DefaultURIResolver());
xsds.setInline(true);
return xsds;
}
以上是关于使用soap web服务在spring boot中加载多个bean的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot Soap Web 服务(Java)——代码优先?
带有 MVC 的 Spring Boot SOAP Web 服务
Spring Boot Soap Web-Service(Java) - 代码优先?