user2821894
user2821894

Reputation: 1088

HTTP Status 405 - Method Not Allowed Error for Rest API

Am asking this question after doing some research. I did followed the solutions given for this kind of error but did not work for me. Any suggestions as where am going wrong in the below code.I am creating a REST API but when I request the url it is giving me the 405 error.Below is the URI am requesting.

    http://localhost:8080/Project/services/start/version

Below is the code snippet.

@Path("/start")

public class StartService {
@GET
@Path("/version")
@Produces({"text/plain","application/xml","application/json"})
public String getVersion() {
    String ver="";

    try{


          Runtime rt = Runtime.getRuntime();
          Process pr = rt.exec("C:\\server\\dgr -v" );

          BufferedReader stdInput = new BufferedReader(new InputStreamReader
(pr.getInputStream()));
          BufferedReader input = new BufferedReader(stdInput);
         // String ver ="";
          StringBuffer verOutput = new StringBuffer();
                while((ver =  input.readLine()) != null){
                    verOutput.append(ver + "\n");
                    System.out.println(ver);
                }


        }catch (Throwable t)  
          {  
            t.printStackTrace();  
          }  


        finally {  

        }
    return ver;  }

}

web.xml:

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

<servlet>
<display-name>eLicensingWeb</display-name>      
    <servlet-name>JAX-RS REST</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
     <init-param>
         <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.cem.plc.service</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>




<servlet-mapping>
    <servlet-name>JAX-RS REST</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>


</web-app>

Upvotes: 48

Views: 431249

Answers (11)

Xavier Soh
Xavier Soh

Reputation: 165

I had the same error, form me, i was using http instead of https.

Upvotes: 0

tRuEsAtM
tRuEsAtM

Reputation: 3668

I have this problem while sending a request from .NET 4.8 WebApp to RestAPI hosted on the same IIS.

A request was sent to the server that contained an invalid HTTP verb. A request was sent to a virtual directory using the HTTP verb POST and the default document is a static file that does not support HTTP verbs other than GET or HEAD. Verify the list of verbs enabled for the module handler this request was sent to, and ensure that this verb should be allowed for the Web site.

Upvotes: 0

Asad Iqbal
Asad Iqbal

Reputation: 11

Issue: "405 Method Not Allowed" error when using PUT or DELETE in an ASP.NET Core application hosted on IIS.

Removing WebDAV ensures that IIS doesn’t block specific HTTP methods, allowing your application to manage them directly.

<modules>
        <remove name="WebDAVModule" />
</modules>

Upvotes: 0

user2721787
user2721787

Reputation: 199

@Produces({"text/plain","application/xml","application/json"})

change this to

@Produces("text/plain")

This is because the headers/content-type being sent to the API must match what it is expecting, or else it will return HTTP 405.

Upvotes: 7

Marco Roy
Marco Roy

Reputation: 5243

In our case the failure was due to sending "Content-Type": "application/json" to an API, when it was expecting "Content-Type": "application/x-www-form-urlencoded" (with the corresponding data format in the body).

Upvotes: 0

Mounir bkr
Mounir bkr

Reputation: 1625

sometimes you forget and PUT instead of GET or vice versa, and you will get this message. juste verify if you use the correst request

Upvotes: 0

Gayathri
Gayathri

Reputation: 165

I had the same issue. In my case the Url had portions of it missing For example :

http://localhost:8080/root/path/action

Instead I had something like

http://localhost:8080/root/action

Take away is check if the URL is correct. In my case I corrected my URL and the issue was resolved.

Upvotes: 2

Vivek Mishra
Vivek Mishra

Reputation: 517

You might be doing a PUT call for GET operation Please check once

Upvotes: 31

Girish
Girish

Reputation: 107

In above code variable "ver" is assign to null, print "ver" before returning and see the value. As this "ver" having null service is send status as "204 No Content".

And about status code "405 - Method Not Allowed" will get this status code when rest controller or service only supporting GET method but from client side your trying with POST with valid uri request, during such scenario get status as "405 - Method Not Allowed"

Upvotes: 6

Gene
Gene

Reputation: 11267

Add

@Produces({"image/jpeg,image/png"})

to

@POST
@Path("/pdf")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces({"image/jpeg,image/png"})
//@Produces("text/plain")
public Response uploadPdfFile(@FormDataParam("file") InputStream fileInputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {
    ...
}

Upvotes: 0

seawave_23
seawave_23

Reputation: 1248

I also had this problem and was able to solve it by enabling CORS support on the server. In my case it was an Azure server and it was easy: Enable CORS on Azure

So check for your server how it works and enable CORS. I didn't even need a browser plugin or proxy :)

Upvotes: 0

Related Questions