Here in this article I'm starting with the basics of Spring Boot Rest API Development. This is going to be an introduction to the series of practical hands-on guides in which we would learn Spring-Boot, Rest API and Microservices.
Prerequisites:-
1% interest to learn
Basics of Java
Basics of Spring & Spring Boot
Create a Spring Boot Project
Lets head-over to https://start.spring.io/ to download the Spring boot project.
Select the build-tool of your choice (Maven/Gradle) and the programming language [Java of course for me :-) ] apart from the Spring Boot version (avoid using Snapshot as they're still in development stage
Then, click on Add dependencies (as highlighted below) on the right hand-side of the page and select "Spring Web" like in the below image
Spring Boot Starter Web Dependency helps to develop Restful Web-Services and Spring MVC Web Apps.
It provides all required dependencies to develop applications. Some of them are
Starter JSON
Spring Web
Spring Web MVC
Apache Tomcat Server (WebServer to run our Spring boot Apps)
No Spring without annotations, isn't it ? As expected, let us go through some of the basic annotations which are used everyday in the world of Spring Boot before heading over to the development.
Useful Annotations:-
@SpringBootApplication - used to define a Spring Boot Application. It is made up of 3 annotations which are
@Configuration - Source of bean definitions
@EnableAutoConfiguration - auto-configures the bean that is present in the class-path and configures it to run the methods present.
@ComponentScan - scans all the packages available in the class-path to find out the beans.
@RestController - This is used in class level to make it as a request handler and used in RESTful Web Services to handle the requests. It in turn is a combination of 2 annotations which are
@Controller - primarily used to make a java class as a Spring MVC Controller
@ResponseBody - The object returned is automatically serialised into JSON & passed back into the HTTP Response object
@PathVariable - retrieves the path parameters from a API request and use it in the controller method
@RequestParam - retrieves the query parameters. I can guess what're you thinking, we will definitely see the differences between this and PathVariable later :)
@RequestBody - binds the http request bodies to method parameters in Spring MVC.
It allows to extract the request body content, such as XML/JSON and map it to the relevant Java object.
Internally uses the Spring provided HttpMessageConverter to convert json into java object.
@ResponseStatus - In order to send a http status in response to the client.
What to expect next:-
Stay tuned for part-2 in which we will go through request-handler methods and start implementing the coding part of Rest-API basics. Please let me know your comments in case of any improvement.
Meanwhile, if you are preparing for Java-Spring Boot developer interviews, then here are a few books from javinpaul & @javarevistited publications
Grokking the Java Interview: https://gumroad.com/a/181345491/QqjGH
Grokking the Spring Boot Interview: https://gumroad.com/a/181345491/hrUXKY