I followed a very nice tutorial and it works smoothly for the GET
http method, but for some reason when I try to access the POST
or PUT
methods the server returns:
HTTP Status 405 - Method Not Allowed
So this is what I did in the tutorial,
That's it, I ran the app on a tomcat 6 app server.
So when I follow the path of my class and I hence a @GET
method it works smoothly but when i try to replace the @GET
annotation with @POST
it return the error above.
The web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>RESTfulTest</display-name>
<servlet>
<servlet-name>NAME</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.RESTful.Test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NAME</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My Java class with the jersey annotations:
package com.RESTful.Test;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/resttest")
public class Test {
//this WORKS!
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getTestString()
{
return "Hello this is a test post";
}
//this returns the error
@POST
@Produces(MediaType.TEXT_PLAIN)
public String getTestString2()
{
return "Hello this is a test post";
}
//this returns the error
@PUT
@Path("{param1}")
@Produces(MediaType.TEXT_PLAIN)
public String getTstWithInput(@PathParam("/param1")
String param)
{
return "hello "+param;
}
//this returns the error
@PUT
@Path(value="/putTest")
@Produces(MediaType.TEXT_PLAIN)
public String getTstWithInput2(@PathParam("/param1")
String param)
{
return "hello "+param;
}
}
Please note that I have tried documenting all but the method I'm testing with the same results. I know I can't run some of them at the same time, they are all just tests.
I'm calling the REST resources from URL:
"http://localhost/RESTfulTest/rest/resttest/"
"http://localhost/RESTfulTest/rest/resttest/myname"
"http://localhost/RESTfulTest/rest/resttest/putTest"
Try like this
return Response.status(200).entity("Sample Response").type(MediaType.TEXT_PLAIN).build();
More Details : http://jersey.java.net/nonav/documentation/latest/user-guide.html
Make sure the rest resources are called properly. Curl can be a hepful tool for testing
curl -XPUT http://localhost/RESTfulTest/rest/resttest/putTest
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.