af_khan
af_khan

Reputation: 1078

Spring Rest method always returning 405 Method not Allowed

I am always getting 405 Method not allowed for a simple GET method.

@RequestMapping(value = "/servicetest")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String checkService() {
    return "Service is up and running";
}

and WEb.xml is like

<web-app 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_3_0.xsd"
          version="3.0">    

    <display-name>Interface</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/application-root-context.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

     <listener>
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>test-if</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>test-if</servlet-name>
        <url-pattern>/testif/*</url-pattern>
    </servlet-mapping>
</web-app>

I am always getting 405 error when i am invoking the service.

Could you please let me know what am doing wrong here ?

Upvotes: 1

Views: 8123

Answers (4)

smile
smile

Reputation: 516

Do you send any additional parameters to the method. Seems like your method parameters don't match Also I am thinking if you don't use @RestController annotation(you used here Responsebody) you should output/return your string/result as json.

Upvotes: 0

Vinit Solanki
Vinit Solanki

Reputation: 2023

you have follow some steps.

  1. if you using Spring Java Based configuration then add CORSFilter

    public class CORSFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {
    System.out
            .println("Filtering on...........................................................");
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods",
            "POST, GET, PUT, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers",
            "x-requested-with, Content-Type");
    chain.doFilter(req, res);
    }
    
    public void init(FilterConfig filterConfig) {
    }
    
    public void destroy() {
    }
    }
    

OR

  1. you are using xml based spring configuration then try to add

<context:annotation-config/> in your Spring configuration file. It will support your controller annotations.

  1. configure @RequestMapping like below example

    @RequestMapping(value = "/servicetest", method = {RequestMethod.GET})
    
  2. still not working ?, then remove header from http call, just make simple http call, if this step not working then you can go for next step.

  3. still not working then don't try to return actual entity just return proxy object or dummy object, if its working then go ahead.

Upvotes: 1

user1857878
user1857878

Reputation:

You have to use method type. Use that in your request mapping.

@RequestMapping(value = "/servicetest", method = {RequestMethod.GET, RequestMethod.POST})

Upvotes: 1

njjnex
njjnex

Reputation: 1555

Try to add <context:annotation-config/> in your Spring configuration file. It will support your controller annotations.

Upvotes: 0

Related Questions