Skip to content

Restful Web Services with Spring Boot Part I

Amitha R edited this page Dec 19, 2020 · 8 revisions

Restful Web Services with Spring Boot

RESTful Web Services

Step 1 - Initializing a RESTful Services Project with Spring Boot

  • start.spring.io
  • Provide details:
    • Maven Project, Java, SpringBoot - 2.0.0(SNAPSHOT)
    • Group : com.example.project
    • Artifact : restful-web-services
    • dependencies : Web , DevTools, JPA, H2
  • Generate Project

  • For any Maven project need to specify a group id and artifact id.

  • Spring Initializer creates a Maven Project, puts it in a Zip and downloaded to the downloads folder.

  • In eclipse -> Import -> Existing Maven Project

  • Load the maven project.
  • Maven would download all the dependencies listed in the pom.xml file.

Step 2: Understanding the RESTful services created

  • REST is a style of software architecture for distributed hypermedia systems (design resources and how you expose them using HTTP).
  • REST makes best use of HTTP.
  • Key abstraction is a resource - each resource has a URI. ** (Users (1) - Posts(Many))
  • Different RESTful Web services we would create.
  1. Retrieve all Users - GET /users
  2. Create a User - POST /users
  3. Retrieve one User - GET /users/{id} -> /users/1
  4. Delete a User - DELETE /users/{id} -> /users/1
  5. Retrieve all posts for a User - GET /users/{id}/posts
  6. Create posts for a User - POST /users/{id}/posts
  7. Retrieve details of a post - GET /users/{id}/posts/{post_id}

Step 3 - Create a Hello World Service

  • returns a getmapping request.

Step 4 - Create a HelloWorldBean request

  • Class object that in turn returns a String.
  • Getter, Setter and toString Method.
  • Without a Getter - encounter an error - No converter found for return value of type.

Step 5 - Quick Review of Spring Boot Auto Configuration and Dispatcher Servlet

  • logging.level.org.springframework = debug
  • AUTO-CONFIGURATION REPORT
  • Consists of a lot of details.
  • Configuration of Dispatcher Servlet.
  • All the configurations are getting fired because of Spring boot auto configuration
  • Spring boot looks at all the classes, all the jars which are available on the classpath and auto configures different things like dispatcher servlet.
  • HTTP message converters - bean is automatically converted to JSON.
  • Jackson to object mapper - converted from JSON to bean and bean to JSON.
  1. Who is configuring dispatcher servlet?
  • Spring Boot Auto configure
  1. How does the HelloWorldBean object get converted to JSON?
  • Jackson to object mapper
  1. Who is configuring the error mapping?
  • Spring Boot Auto Configuration.
  • Dispatcher Servlet is handling all the requests. (/)
  • Follows a pattern called front controller.
  • Dispatcher Servlet is the front controller for Spring MVC framework.
  • Dispatcher Servlet knows the various mappings present in the application.
  • Once the dispatcher servlet gets the request, it determines which is the right controller to execute that request.
  • Once the bean is returned, dispatcher servlet looks at how do I send the response back.
  • Dispatcher Servlet will make use of Jackson to perform the conversion to JSON.
  • The helloworldbean is converted to JSON and sent back.

Step 6 - Hello World service with a path variable.

Step 7 - Creating User Bean and User Service.

Step 8 - Implementing GET methods for User Resource.

Step 9 - Implementing POST Method to create User Resource.

Step 16 - Implementing HATEOAS for RESTful Services

  • HATEOS : Hypermedia as the engine of application state.
  • In POM.xml file add the following dependency

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-hateoas</artifactId>

</dependency>