Spring Data JPA: to connect our REST API to a database. hence we can use Hibernate/JPA for this.
REST is a style of software architecture for distributed hypermedia systems
=========================================================================
# RESTful Web Services
Social media Application
User make many posts. hence, the relationship b/w user and posts is one to many.
- Retrieve all Users - GET /users
- Create a User - POST /users
- Retrieve one User - GET /users/{id}
- Delete a User - DELETE /users/{id}
- Retrieve all posts for a User - GET /users/{id}/posts
- Create a posts for a User - POST /users/{id}/posts
- Retrieve details of a post - GET /users/{id}/posts/{post_id}
- We have used annotation for RestController
- Then mapped GET request to the URI to the above method.
- Now, whatever my method will return, is displayed on the browser.
- Also, we can use GetMapping, instead of RequestMapping.
========================================================================
Step 1. Enhancing the Hello World Service to return a Bean
This error means "No convertor found for return value of type".
So, If we don't create a GETTER, automatic conversion will not work.
This happened because there is not getters in HelloWorldBean class.
So, variable, "message" is coming back as JSON string
=======================================================================
Now we will understand what's happening in the background?
Who is handling the request?
How HelloWorldBean content getting translated to JSON?
Step 2. RQuick Review of Spring Boot Auto Configuration and Dispatcher Servlet - What's happening in the background?
What is dispatcher servlet?
who is configuring dispatcher servlet?
What does dispatcher servlet do?
How does the HelloWorldBrean object get converted to JSON?
Who is configuring the error mapping?
============================
CONDITIONS EVALUATION REPORT
============================
This has lot of details of AutoConfiguration reports.
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
Above says, "DispatcherServletAutoConfiguration matched, because it found a class called DispatcherServlet "
- We added a starter on SpringBoot Starter Web
- SpringBoot Starter Web has dependency on spring web MVC framework
- Hence, we got DispatcherServlet class into our classpath.
- So, SpringBoot Autoconfiguration says, "I found DispatcherServlet on the classpath"
- Hence, now we can configure the DispatcherServletAutoConfiguration
ErrorMvcAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
- found 'session' scope (OnWebApplicationCondition)
It says "javax.servlet.Servlet classes are found on classpath, So we would need to configure error page"
ErrorMvcAutoConfiguration#basicErrorController matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorController; SearchStrategy: current) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration#errorAttributes matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorAttributes; SearchStrategy: current) did not find any beans (OnBeanCondition)
ErrorMvcAutoConfiguration.DefaultErrorViewResolverConfiguration#conventionErrorViewResolver matched:
- @ConditionalOnBean (types: org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) found bean 'dispatcherServlet'; @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; SearchStrategy: all) did not find any beans (OnBeanCondition)
So, its configuring the basicErrorController, errorAttributes and conventionErrorViewResolver.
The error page that we saw earlier was White label error page.
WhitelabelErrorViewConfiguration has generated the error page.
So, all these configurations are getting fired because of something called SpringBoot AutoConfiguration.
SpringBoot looks at all the classes, all the jars which are available on the classpath, and based on whatever is in the classpath, it tries to auto-configure different things like DispatcherServlet.
HttpMessageConvertersAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.converter.HttpMessageConverter' (OnClassCondition)
- NoneNestedConditions 0 matched 1 did not; NestedCondition on HttpMessageConvertersAutoConfiguration.NotReactiveWebApplicationCondition.ReactiveWebApplication did not find reactive web application classes (HttpMessageConvertersAutoConfiguration.NotReactiveWebApplicationCondition)
Other things that are autoconfigured are HTTP Message Converters. So, these HTTP message converters are responsible for converting to JSON.
What is happening is called Jackson to object mapper.
JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder' (OnClassCondition)
Jackson to object mapper does the conversion from JSON to beans and beans to JSON.
Who is configuring Dispatcher Servlet?
Ans. SpringBoot AutoConfiguration
How does the HelloWorldBrean object get converted to JSON?
Ans. Because of SpringBoot AutoConfiguration as Message Converters and Jackson beans are getting initialized.
Who is configuring the error mapping?
Ans. SpringBoot AutoConfiguration, creates a default error page for us.
==========================================================================
Mapping servlets: dispatcherServlet urls=[/], webServlet urls=[/h2-console/*]
[/] this is the root of the web applications
Dispatcher servlet do 2 things:
- convert to JSON
- sends back the response
- When you type the URL /hello-world-bean, the request goes to DIspatcherServlet
- DispatcherServlet will be the one which is handling the request first.
- This follows the pattern called FRONT CONTROLLER.
- DispatcherServlet is the front controller for spring MVC Framework.
- So request goes to DispatcherServlet
- DispatcherServlet knows all the different mapping present in the application
- It knows /hello-world2 and /hello-world-bean GET method is mapped to what specific method
- Then dispatcher servlet finds the HelloWorld Controller,
- It calls specific method helloWorldBean(), gets the object, invokes the conversion on it,
- So, once, DispatcherServlet gets the request, it determines that what is the right controller to execute the request, and will execute the right method of the controller

- Once object is returned back, then dispatcherServlet will look how do I send response back.
- FOr this we have @RestController annotation. part of it has @ResponseBody
- What would happen when we put ResponseBody on a controller?
- the response from it would be mapped by message convertor to some other format.
- Here, we use Jackson as message convertor
- Converts it into JSON and then response back.
- So, DispatcherServlet will say do the JSON conversion using jackson







No comments:
Post a Comment