如何使用 spring 注释配置 MessageDispatcherServlet 和 ServletRegistration

How to config MessageDispatcherServlet and ServletRegistration both by using spring annotaion

我已经为 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 class 是

@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();
  }

}

我使用 Spring 引导配置解决了我的问题,这有助于我调用 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"));
}