How To Resolve 405 – Method Not Allowed While Calling REST Service

Discipline is the bridge between goals and accomplishment. ~ Jim Rohn

One of the common errors you might encounter while accessing REST services is 405 Method Not Allowed error.  So what does this mean?  And how can you resolve it?

What Does 405 – Method Not Allowed means?

To understand this you need to know little bit about HTTP methods.  There are four basic HTTP methods.  GET, POST, PUT and DELETE.  There are other methods but these are the common ones.  Out of these four the GET and PUT methods are most frequently used.

REST services communicate with client on HTTP protocol and client uses one of these methods to make a service call.  Usually if you want to fetch some data from the server you will use GET method.  So save some data POST method is used.  PUT is used for update if found else insert data.  DELETE is used to delete data from the server.

The method which is served by the REST service is mentioned at the service code.  The client accessing the service should call the service with the same coded.

For example if there is a GET method for a service, the client should also send GET request for the URI.  If the method sent by the client is not the same as the server then 405 Method Not Allowed exception is returned to the client.  This is not only specific to REST.  It is part of HTTP specification.

Here is an example

package com.freetipscentral;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.freetipscentral.domain.Book;

@Path("/getResponseJSON")
public class RestJSONResponseService {

  @GET
  public Response getBookAsXML(@Context HttpHeaders httpHeaders) {
    
    Book book1 = new Book("Head First Java", 1);
    
    return Response.ok(book1,MediaType.APPLICATION_XML).build();
  }
}

As you can see above the service expects to be called with GET method as it is annotated with @GET method.

If the above service is called with any other method such as POST or PUT you will see the following exception.

<html>
    <head>
        <title>Apache Tomcat/7.0.65 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 405 - Method Not Allowed</h1>
        <HR size="1" noshade="noshade">
        <p>
            <b>type</b> Status report
        </p>
        <p>
            <b>message</b>
            <u>Method Not Allowed</u>
        </p>
        <p>
            <b>description</b>
            <u>The specified HTTP method is not allowed for the requested resource.</u>
        </p>
        <HR size="1" noshade="noshade">
        <h3>Apache Tomcat/7.0.65</h3>
    </body>
</html>

How To Resolve 405 Method Not Allowed

You would usually need to change the method with which you are calling the service.  If you own the service code then you can look up the method and see the method such as GET  or PUT and call with that method.  If you are calling a third party service you can look up the documentation and then call with that method.