1

I've recently been trying to implement a simple healthcheck service for our Spring project using Spring MVC with a JSP webpage. This is my first time working with Spring MVC and I've been working through this Spring MVC tutorial. The problem seems to be that when it is trying to render my page, it returns a 405 and I don't understand why. If someone could help me out that would be greatly appreciated.

Right now I have a servlet and mapping setup as follows in my web.xml:

<servlet>
  <servlet-name>health</servlet-name>
  <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>health</servlet-name>
  <url-pattern>/health</url-pattern>
</servlet-mapping>

And I have my health-servlet.xml configured as so:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

<context:component-scan base-package="com.mycompany" />

<import resource="classpath*:applicationContext.xml" />

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

I also have a simple controller setup:

@Controller
public class HealthcheckEndpoint {
    @RequestMapping(value = "/health", method = RequestMethod.GET)
    public String checkHealth(ModelMap mav) {
        // Some business logic
        return "healthcheck";
    }
}

Also, I do have a .jsp page setup in WEB-INF/health/healthcheck.jsp.

I have turned on org.springframework.web logging and these are the last 2 lines before I get the 405 response:

[2012-01-31 12:52:04,770] DEBUG - org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(236) | Forwarding to resource [/WEB-INF/health/healthcheck.jsp] in InternalResourceView 'healthcheck'
[2012-01-31 12:52:04,780] DEBUG - org.springframework.web.servlet.FrameworkServlet.processRequest(674) | Successfully completed request

Update

Also defined in my web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext-resources.xml</param-value>
</context-param>

And in my applicationContext-resources.xml I do have the line context:annotation-config.

Note: This health servlet is an addition to our already existing service. Hence, now there are 2 org.springframework.web.servlet.DispatcherServlet servlets defined in our web.xml. I don't know if this is a problem or not, but it seems to work because I can debug into both endpoints.

2
  • maybe you can change '/health' to '/health/*' in your web.xml under servlet-mapping tag. Also try network capturing with firebug or any other explorer tool to see if you try any post method.
    – HRgiger
    Jan 31, 2012 at 19:15
  • @HRgiger Changing to servlet-mapping had the same result. Also, when I monitored the network with Firebug, it said there was only 1 request which was my GET.
    – Dan W
    Jan 31, 2012 at 19:39

3 Answers 3

2

A 405 "Method not allowed" means the HTTP method (GET, POST, etc.) used on the resource (e.g. /health) is not supported by the server. You've clearly indicated that GET is supported (in your controller) so my guess is you are hitting your URL with the wrong HTTP method (POST perhaps?).


EDIT: So you are doing a GET.

On further examination of your configuration I believe you need a <context:annotation-config/> line that tells Spring to process annotations (like on your controller) allowing it to know about your GET support. Perhaps try that?

3
  • The way I am trying to access this is by putting a URL into my browser: localhost:8080/myapp/health
    – Dan W
    Jan 31, 2012 at 19:35
  • OK. That's not it then. I've added another suggestion to my answer.
    – SingleShot
    Jan 31, 2012 at 19:40
  • I have updated my answer to clarify that we do have that line.
    – Dan W
    Jan 31, 2012 at 19:52
1

According to your configuration, behavior description and log file, I would guess that the problem is not that jsp in first place. I would guess that your JSP (HTML) loads some other resources that trigger the 405.

0

Despite what my note says, one of the other servlets was eating the output by this url request. I have imported this URL re-write tool and everything is now working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.