Ted Tomlinson
Ted Tomlinson

Reputation: 783

Spring MVC on tomcat 7 gives HTTP 405 for requests without a path

I'm having this issue with Spring MVC on Tomcat that I think was the same one addressed in this post but they didn't post the solution.

Web.xml

<!--  Spring MVC app -->
  <servlet>
    <servlet-name>client</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/client/client-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>client</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping> 

client-servlet.xml

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

  <context:component-scan base-package="com.company.client.controllers" />

  <!-- Enable an interceptor to set up the Trace objects for all Controller invocations -->
  <mvc:interceptors>
     <bean class="com.company.client.interceptor.TraceInterceptor"/>
  </mvc:interceptors>

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

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

I use a standard ContextLoaderInitializer.

My controller uses @RequestMapping(value = {"", "/*"}, method = RequestMethod.GET)

 @RequestMapping(value = {"", "/*"}, method = RequestMethod.GET)
  public String home(Model model) {
    retrieveOffers(model); 
    logger.warning("Loading home page");
    return "main";
  }

When i visit mydomain.com or mydomain.com/ - I get the Tomcat 405 GET not supported error. If I put anything in the trailing path it loads the page just fine. (e.g. mydomain.com/a)

The interesting thing is that I always see the "Loading home page" log statement and from my logs there is no difference between requests. It just seems like Tomcat is intercepting the response sometime between the controller finishing and the view rendering. Any thoughts?

Upvotes: 1

Views: 1456

Answers (2)

Ted Tomlinson
Ted Tomlinson

Reputation: 783

there was a statement in the web.xml that somebody on my team had added. It intercepted response after the controller and redirected to a servlet that didn't implement doget. Hopefully this save somebody some time later on. Use the following to ensure the default servlet container does not override spring.

  <!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>

Upvotes: 2

jokr
jokr

Reputation: 242

It rather looks like Spring can't resolve the view properly. When your controller method returns a simple String, it looks for an according view pre- and suffixed with the values from your dispatcher servlet config.

Is there a InternalResourceViewResolver configured? Like the following:

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Then at the end of your controller, Spring would look for a main.jsp file in your WEB-INF folder (it takes the String "main" and adds the prefix and the suffix).

is this all correct? Could you otherwise provide the spring config?

Upvotes: 0

Related Questions