Spring Boot RESTfull web service
In Previous post I explained how to get started with Spring boot. In this post we will go through how to create RESTfull web services with Spring boot.
Developing Your First RESTfull Application
let’s use https://start.spring.io/ web site to generate the base for our project.
Provide basic details like Project name, Java version, spring boot version and packaging type etc..
Click on Add dependencies and select Spring Web (Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.)
pom.xml now contains spring-boot-starter-web adds RESTfull functionality to our project, and uses Apache Tomcat as embedded container
Download and import the project to your eclipse. Eclipse will automatically download all required dependencies.
Open Maven Dependencies, you will find dependent jars like tomcat-embed, logback, Jackson etc..
Create package named controller to keep our resources/controllers
Create a class called BooksController.java
BooksController is annotated with @RestController
1.) @RestController indicates that the data returned by each method will be written straight into the response body instead of rendering a template.
2.) Routes for each operation (@GetMapping
, @PostMapping
, @PutMapping
and @DeleteMapping
, corresponding to HTTP GET
, POST
, PUT
, and DELETE
calls)
Run our application and point to this url on your browser: http://localhost:8080/books.
Bonus Point
if you like to run your server on different port instead of default 8080 port, just add below line in your application.properties file
server.port=8081
Conclusion:
What needs to be done to make the REST architectural style clear on the notion that hypertext is a constraint? In other words, if the engine of application state (and hence the API) is not being driven by hypertext, then it cannot be RESTful and cannot be a REST API.
Spring HATEOAS, a Spring project aimed at helping you write hypermedia-driven outputs
If you would like to refer to the full code, do check https://github.com/projectk-user1/Springboot-learning.git
Feel free to provide your suggestions in comments section