Spring Tomcat部署:Soap调用返回“405:Method not allowed”

时间:2015-05-31 22:15:24

标签: spring spring-mvc tomcat spring-ws

我正在运行一个发布REST和SOAP服务的webapp应用程序。 当我通过gradle run(spring-boot插件)运行我的应用程序或作为STS的Java应用程序时,它们都工作正常。每当从gradle run或STS运行应用程序时,从SoapUI调用SOAP服务或从index.html调用 $。soap ajax调用时,SOAP服务都能正常工作。 我刚刚尝试在Tomcat 7上部署我的webapp.Webapp静态内容和REST服务工作正常。问题是SOAP服务不起作用。

我从index.html $。soap 调用或SoapUI测试中获得以下错误代码:

  

POST http://localhost:8080/soap/ 405(方法不允许)

这是我的 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 带注释的类的结构:

@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) {

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

    }
}

可能原因是Spring拒绝对SOAP服务的POST请求。有没有办法启用它们?我的所有REST服务都是GET请求,但我知道 @RequestMapping 带注释的方法可以注释为GET或POST请求侦听器。但是,对于我的肥皂服务,我只是不知道。

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

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

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。 我真的错过了关键点。我所有的肥皂服务都使用了一些 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运行,这些服务工作正常。问题是我的 web.xml 文件中没有定义这个servlet。现在该文件看起来像这样:

<?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>