Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Status 405 - Method Not Allowed Error for Rest API

Tags:

java

rest

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> 
like image 500
user2821894 Avatar asked Oct 02 '13 18:10

user2821894


People also ask

Why am I getting a 405?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

How do you fix 405 Method not allowed in Postman?

If you are certain you need a POST request, chances are, you're just using the wrong endpoint on the server. Again, this can only be solved if you have proper documentation to show what methods are supported for each endpoint.


2 Answers

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

like image 195
Vivek Mishra Avatar answered Oct 09 '22 06:10

Vivek Mishra


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"

like image 41
Girish Avatar answered Oct 09 '22 05:10

Girish