如何使用spring annotaion配置MessageDispatcherServlet和ServletRegistration

时间:2018-04-12 07:14:42

标签: spring configure

我为SOAP服务配置了MessageDispatcherServlet,为Web服务配置了ServletRegistration,但是在Web服务的情况下没有调用控制器。

public class WebAppInitializer implements WebApplicationInitializer  {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

    // use MessageDispatcherServlet instead of standard DispatcherServlet for SOAP messages
     MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setContextClass(WebServiceWsConfig.class); 
    servlet.setApplicationContext(context);
    servlet.setTransformWsdlLocations(true);

    // register MessageDispatcherServlet as Web Service entry point
    final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("MessageDispatcherServlet",servlet);
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/soapws/*");
    dispatcher.addMapping("/");

  }
}

我的WebServicesConfig类是

@Configuration

@EnableWs

@EnableWebMvc

@ComponentScan(basePackages = "")

@PropertySource(value = {"classpath:config_local.properties"})


public class WebServiceConfig extends WebMvcConfigurerAdapter {

  @Autowired
  private Environment env;

  @Bean(name = "pos")
  public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema posSchema) {

    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("posPort");
    wsdl11Definition.setLocationUri("/soapws");
    wsdl11Definition.setTargetNamespace("http://---.---.in/soap");
    wsdl11Definition.setSchema(posSchema);
    return wsdl11Definition;
  }

  @Bean
  public XsdSchema studentsSchema() {
    return new SimpleXsdSchema(new ClassPathResource("pos.xsd"));
  }
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }

}

1 个答案:

答案 0 :(得分:2)

我使用Spring Boot配置解决了我的问题,这有助于我调用Soap服务以及Web服务。

@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 = "pos")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema posSchema) 
{

    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("posPort");
    wsdl11Definition.setLocationUri("/soapws");
    wsdl11Definition.setTargetNamespace("http://---.---.in/soap");
    wsdl11Definition.setSchema(posSchema);
    return wsdl11Definition;
}

@Bean
public XsdSchema studentsSchema() {
    return new SimpleXsdSchema(new ClassPathResource("pos.xsd"));
}