分离Spring-WS和Spring-Webmvc

时间:2016-01-18 05:35:45

标签: java web-services spring-mvc spring-boot

使用spring-ws执行基于SOAP的应用程序。但是,如果我添加以下依赖项(从spring-boot教程https://spring.io/guides/gs/producing-web-service/中看到),

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

它还会提取spring-webmvc。如果我将其排除,

        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </exclusion>
        </exclusions>

我在这里得到错误;

@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    //ERROR Here
    //cannot find symbol
    //symbol:   method setApplicationContext(ApplicationContext)
    //location: variable servlet of type MessageDispatcherServlet
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/ws/*");
}

他们不是分开模块吗?当我只需要spring-webmvc时,为什么我必须使用spring-ws

我在这里不明白?

1 个答案:

答案 0 :(得分:2)

spring-ws-core需要spring-webmvc,你无法避免这种情况,因为一些核心的Spring-WS类是在Spring-WebMVC类(包括MessageDispatcherServlet)之上构建的。

spring-ws-core POM定义了对spring-webmvc

的显式依赖关系

来自https://repo1.maven.org/maven2/org/springframework/ws/spring-ws-core/2.2.4.RELEASE/spring-ws-core-2.2.4.RELEASE.pom

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.0.9.RELEASE</version>
  <scope>compile</scope>
</dependency>

在maven中添加排除项很少是一个好主意 - 有时您需要这样做,但是您几乎都说您认为您比包装程序更好地理解库依赖项,而您可能不会。

对于错误消息,MessageDispatcherServlet继承自org.springframework.web.servlet.FrameworkServletspring-webmvc打包。在setApplicationContext上定义的FrameworkServlet方法,但仅在 Spring-WebMVC 的4.x版本中添加。

当您添加排除时,从setApplicationContext添加到FrameworkServlet之前,看起来效果是您最终使用旧版本的spring-webmvc。