Spring Tomcat 部署:Soap 调用 returns“405:方法不允许”

Spring Tomcat Deployment: Soap call returns "405: Method not allowed"

我运行正在开发一个发布 REST 和 SOAP 服务的网络应用程序。 当我通过 gradle 运行(spring-引导插件)或作为来自 STS 的 Java 应用程序 运行 我的应用程序时,它们都工作正常。 SOAP 服务在从 SoapUI 调用或从 $.soap ajax 调用时完美运行,只要应用程序是 运行 来自 gradle 运行 或 STS。 我刚刚尝试在 Tomcat 7 上部署我的 webapp。Webapp 静态内容和 REST 服务运行良好。问题是 SOAP 服务不工作。

我收到以下错误代码,来自 index.html $.soap 调用或来自 SoapUI 测试:

POST http://localhost:8080/soap/ 405 (Method Not Allowed)

这是我的 servlet-context.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<!--    DispatcherServlet Context: defines this servlet's request-processing
    infrastructure

    Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

<!--    Handles HTTP GET requests for /resources/** by efficiently serving up
    static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

<!--    Resolves views selected for rendering by @Controllers to .jsp resources
    in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="com.stw_2015.app" />
</beans>

这是我的 @Endpoint 的结构注释 class:

@Endpoint
public class SoapController {
    private static final String NAMESPACE_URI = "http://com.stw_2015.app/soap";
    private static final Logger logger = LoggerFactory.getLogger(SoapController.class);


    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "registerActionRequest")
    @ResponsePayload
    public RegisterActionResponse registerAction (
            @RequestPayload RegisterActionRequest registerActionRequest) {

        ................

    }
}

原因可能是 POST 对 SOAP 服务的请求被 Spring 拒绝了。有什么办法可以启用它们吗?我所有的 REST 服务都是 GET 请求,但我知道 @RequestMapping 注释方法可以注释为 GET 或 POST 请求侦听器。但是,对于我的 Soap 服务,我就是不知道。

有人可以帮帮我吗? 谢谢!

PD: 请告诉我是否应该粘贴其他文件内容。

我设法解决了这个问题。 我真的错过了关键点。我所有的 Soap 服务都使用一些 MessageDispatcherServlet bean 定义如下:

@EnableWs
@Configuration
public class SoapWebServicesConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/soap/*");
    }

    // http://localhost:8080/soap/soap_description.wsdl -> Accesible
    @Bean(name = "soap_description")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema actionsSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("SoapPort");
        wsdl11Definition.setLocationUri("/soap");
        wsdl11Definition.setTargetNamespace("http://com.stw_2015.app/soap");
        wsdl11Definition.setSchema(actionsSchema);
        return wsdl11Definition;
    }

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

正如我所说,如果来自 STS 运行 或 gradle 运行,这些服务工作正常。问题是这个 servlet 没有在我的 web.xml 文件中定义。现在该文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/soap/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

在此之前,appServlet servlet(配置为 servlet-context.xml 我之前展示过)试图处理“/soap/*”请求。现在,spring-ws servlet 正在处理这些请求,而 appServlet 正在处理其余请求。

最后,我不得不创建一个这样的文件,名为 spring-ws-servlet.xml,位于 "WEB-INF" 文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    <context:component-scan base-package="com.stw_2015.app" />
</beans>