4

i am using restful webservices I have a simple code as given below:

@Path("/v1/status")
public class ControllerServices 
{
    @GET
    @Produces(MediaType.TEXT_HTML)
    String printOnly()
    {
        System.out.println("running successfully");
        return "<p>this webservice</p>";
    }


}

And my web.xml file goes like this:

    <servlet>
    <servlet-name>Jersey REST Service</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.techbloomer.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
   <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

When I request

http://localhost:8080/webservicesForIndTadka/rest/v1/status

it gives the error as

HTTP Status 405 - Method Not Allowed
type: Status report
message: Method Not Allowed
description: The specified HTTP method is not allowed for the requested resource.
Mayur Gupta
  • 762
  • 3
  • 8
  • 23
  • 1
    What is `com.techbloomer.services`? –  Oct 12 '13 at 14:32
  • 1
    What server are you using? Maybe this [link](http://stackoverflow.com/questions/6585211/java-jersey-rest-no-provider-classes-found-404-error-when-accessing-url) can help you –  Oct 12 '13 at 14:42
  • How do you do he test request ? With a browser ? First idea: you do not do a GET request ... – PeterMmm Oct 12 '13 at 16:05
  • In case you use FireFox as WebBrowser, you can install the `HttpFox` plugin which will sniff the HTTP request and response headers for you. --> Just for verification, how your request looks like. A further plugin called `RESTClient` will allow you to create custom HTTP requests. – My-Name-Is Oct 12 '13 at 16:25
  • @Xcito i am using vmware vfabric 2.8 server – Mayur Gupta Oct 13 '13 at 07:22

5 Answers5

1

I've seen a similar problem, when a GET request has a Content-Type in header, tomcat (version 7 in my case) returns error code 405.

ren78min
  • 66
  • 6
0
@Path("/v1/status")
public class ControllerServices 
{
    @GET
    @Path("print")
    @Produces(MediaType.TEXT_HTML)
    public String printOnly()
    {
        System.out.println("running successfully");
        return "<p>this webservice</p>";
    }
}

The path annotation on the class indicates that this is a root resource class and the path value given specifies the base URI for all the web service methods contained in the class.

The @GET annotation is used to differentiate between a sub-resource method that handles the actual web service request and a sub-resource locator method that returns an object that will instead be used to handle the request. In this case, the method has the @GET annotation which means this method handles the request and returns the result.

If you navigate to http://localhost:8080/webservicesForIndTadka/rest/v1/status/print/ you should see printOnly returning "<p>this webservice</p>".

Use FireFox and install RESTClient

Mayur Gupta
  • 762
  • 3
  • 8
  • 23
  • The @Path annotation on the WebService method isn't necessarily! So this maybe won't solve the problem. – My-Name-Is Oct 13 '13 at 11:56
  • 1
    Of course it's not necessary. It was only example code with explanation. But I do see in Mayur Guptas code that his webservice method is private. Can that be the problem? –  Oct 13 '13 at 12:31
  • 1
    I mean it should be `public String printOnly(){ ... }` –  Oct 13 '13 at 12:39
  • 1
    You are right! The method needs to be public. The service method in Mayur Gupta example is package private, which is not sufficient. – My-Name-Is Oct 13 '13 at 14:04
  • @Xcito , thanx for your help but still its not working ..... i just copy past your code... it says: "The requested resource is not available." – Mayur Gupta Oct 14 '13 at 13:04
  • @MayurGupta, this is the last thing I can think about without actually debugging the code. Try changing to `@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON})` –  Oct 14 '13 at 18:26
  • @Xcito.. thanx xcito .. i appriciate your answers... if i got the solution i will notify you... – Mayur Gupta Oct 15 '13 at 04:41
  • @Xcito i got the solution ... there was some incomplete name space in the xml file, including your point to make the service method public. thanx. – Mayur Gupta Oct 15 '13 at 10:36
  • @My-Name-Is thnx for pointing out the mistake. its working now. – Mayur Gupta Oct 15 '13 at 10:37
0
  1. Your browser is sending a different content-type to the JAX-RS service.

Try

@Produces(
    {
        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
    })
  1. You should add

the following

@Path("/get")

for specifying the path to the method, JAX-RS may be using something different.

dendini
  • 3,842
  • 9
  • 37
  • 74
0
http://localhost:8080/webservicesForIndTadka/rest/v1/status

I just had a similar problem and it boiled down to a missing / at the and of the request. In this case it would be

http://localhost:8080/webservicesForIndTadka/rest/v1/status/
ralgrad
  • 29
  • 10
0

hi i have encountered the same problem but fixed it.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Manoj Krishna
  • 347
  • 1
  • 4
  • 12