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.
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=""
xmlns:xsi=""
xsi:schemaLocation=" "
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> 8 7 Answers
You might be doing a PUT call for GET operation Please check once
1In 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"
@Produces({"text/plain","application/xml","application/json"}) change this to @Produces("text/plain") and try,
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 :)
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 { ...
} I had the same issue. In my case the Url had portions of it missingFor example :
Instead I had something like
Take away is check if the URL is correct. In my case I corrected my URL and the issue was resolved.
When I got 405 when making a rest call, I did all research and none worked for me. Finally I realized I did not put the right version of code in tomcat. Once the code with the method is in place, the rest call worked. Just want to put here FYI. Please check your version of code first!!!!