Simple GET method implementation in RESTful Java

3
1520
Learn on Udemy Today!

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.

GET implementation:

[1] – Path of the parent resource

[2] – Path of the sub-resource

[3] – Method indication. Here it is a GET method annotated with @GET

[4] – 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.

Read also: Packaging and deploying our web services

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…

SHARE
Previous articleBluemixathon: An IBM hackathon for developers
Next articleMarshalling Java to JSON, never be hard anymore!
Thamizharasu is a Java developer living in India. He is writing about Java programming, RESTful web services and frameworks. He also loves working with Web API, Elastic search and Firebase. Currently he is a Madras JUG lead and you can follow him on Twitter @zarub2k