Step 1 Rest With Spring Boot
How to Build Rest API
- Identify Resources: (/user, /user/{id}) etc.
- Identify Actions (GET, POST, PUT, DELETE, PATCH etc.)
- Defining Request and Response Structure
- Define Response Status( 200 as Ok, 404 as Page Not Found, 500 Internal Server Error)
- 400 Series Errors: Client Side Error
- 500 Series Errors: Server Side Error
Best Practices for Rest API
1.Thinking on Consumer Perspective
2. validation
3 Internationalizations -i18n
4. Exception Handling
5. HATEOAS
6. Versioning
7. Documentation8. Content Negotiation etc.
Internal Working of Spring Boot
1. How
to Handle Request in Spring Boot?
Dispatcher
Servlet- Front Controller Pattern
Mapping Servlets:
dispatcher Servlet urls=[/] ( as
dispatcher servlet is mapped to the [/] root URL)
If you search for Mapping servlets you will
find out the given below string in logs which is telling to you dispatcher
servlet is mapped with root [/] URL
First
Request Goes to the dispatcher servlet once request receive to the Dispatcher
servlet than it would map the request to the right controller.
Who is
Configuring Dispatcher Servlet:
With the
help of autoconfiguration if you search for dispatcherservletautoconfiguration
in the log
You should
see a bean which is being created.
How does
the Object Convert in to JSON in Spring Boot:
Step 1. request
goes to dispatcher servlet.
Step 2. The
dispatcher servlet would check what are the URLs.
Step 3. Then
find the particular / slash and name of your URL to map the method with your Request.
Step 4. Will
Execute the method and Return Object.
2. How
the Object convert into JSON?
There is
two Important Part are involved to handle this.
1. @ResponseBody
2. JacksonHttpMessageConverters
@ResponseBody:
If you go inside of @RestController you will find @ResponseBody inside of RestController interface
@ResponseBody
Annotation inside of our @RestController
The default conversion, which is set
up by Spring boot auto configuration, is using Jackson HTTP
JacksonHttpMessageConverters: it is part of Autoconfiguration.
autoConfiguation
(JacksonHttpMessageConvertersConfiguration) if you search for JacksonHttpMessageConvertersConfiguration
this is actually configured for REST API by spring boot
So the
Object Conversion into JSON with the help pf @ResponseBody which is inside of
our @RestController Annotations that jackSon HTTP Message Converters are auto
configured by spring boot
3. Who
is configuring Error Mapping?
In given
below image we have given wrong url and we got 404 error
Also says this
application has no explicit mapping for /error, so you are seeing this as a fallback.
So here
also error mapping handle by Autoconfiguration
Autoconfiguration
(ErrorMvcAutoConfiguration)
If you
search for ErrorMvcAutoConfiguration you
will find this in logs also the class will have the message which you have seen
above
4. How
All jar is available (Spring, spring MVC, Jackson, Tomcat etc.)?
Starter
Projects: spring boot started web (spring-webMvc, spring-web, spring=boot-starter-tomcat,
spring-boot-starter-Json)
If you can
go through Spring-boot-starter-web_VersionX.pom file
<name>spring-boot-starter-web</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
This shows
all the different dependencies jar
Tomcat, Json
and other webMvc, web
Comments
Post a Comment