We are going to see how we to implement HTTP GET method in RESTful Java. Before we move further I would recommend you to read the overview of HTTP Methods.
As I explained already, GET method is used to fetch entity information from the given resource URI. First we will see some working code before we deep dive into the explanation. Since this is our first web service implementation, I have ignored the complexity in the below example.
 – Path of the parent resource
 – Path of the sub-resource
 – Method indication. Here it is a GET method annotated with @GET
 – Content type of the response. In our case it is a simple text output. This can be anything like JSON, XML
Resource auto discovery:
Resource classes should be registered in-order to make it is auto discovered by the web container (Our case Tomcat). There 2 ways we can register our resource file into web.xml
- Register resource file through custom Application (javax.ws.rs.core.Application)
- Bind the resource package in web.xml
In our example we will see the second approach of enabling the resources. These bindings should be done in our web.xml. So our web.xml file should be like,
Here <param-value> indicates that, the class files available under the package will be auto deployed in the define resource URI.
The above implemented RESTful web service can be accessed through http://localhost:8080/restskol/api/books/hello
[restskol] – context name
[api] – servlet mapping path
[books] – parent resource path
[hello] – sub-resource path
Now you will see the expected output in your browser if you access the above resource path.
I hope you have enjoyed this article. Please share this with your friends and subscribe to our newsletter for getting the latest updates from us!
Please refer https://github.com/cloudskol/restskol for complete source code…