Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
203 views

Spring Boot

Spring Boot is a framework that helps in creating stand-alone, production-grade Spring applications that can be "just run". It is popular because it speeds up development, provides an easier way to configure and run web and simple applications without needing to deploy WAR files or configure web servers manually. Spring Boot favors convention over configuration and is built on the Spring framework with features like embedded servers, starter dependencies, and auto-configuration of Spring.

Uploaded by

Snehal Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

Spring Boot

Spring Boot is a framework that helps in creating stand-alone, production-grade Spring applications that can be "just run". It is popular because it speeds up development, provides an easier way to configure and run web and simple applications without needing to deploy WAR files or configure web servers manually. Spring Boot favors convention over configuration and is built on the Spring framework with features like embedded servers, starter dependencies, and auto-configuration of Spring.

Uploaded by

Snehal Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

What is Spring Boot?

Spring Boot is a combination of two words “Spring” and “Boot”. Spring Boot is
an open-source java-based framework that helps in bootstrapping the Spring
Application.
“Spring Boot makes it easy to create stand-alone, production-grade Spring
based Applications that you can just run”.
Why is Spring Boot so popular?
1. Spring Boot speeds up the project development.
2. It provides an easier and faster way to configure, set up & run both web-
based as well as simple applications.
3. No web servers like (Tomcat, Jetty, Undertow) are required to configure
separately. Therefore, no need to deploy any war file manually.
4. Reduces XML configurations and promotes annotations.
5. Combines more than one annotation of Spring framework & replaces them
by introducing a single annotation.
For example, @SpringBootApplication is a combination of three annotations -:
@EnableAutoConfiguration, @Configuration, @ComponentScan
9. Automatically configures Spring.
11. Provides easy integration mechanism with many third party tools.
13. Spring Boot Application easily integrates with its ecosystem like Spring
JDBC, Spring ORM, Spring Data, Spring Security, Spring Web MVC etc.
14. It integrates with databases very easily because its internally integrate a
spring data JPA.

What are the advantages of Spring Boot?


1) Spring Boot Starters
provides to add ready-made mini starter projects to simplify the process of
development. Like as Spring Web, Spring Data JPA, MySQL Driver, Eureka
Server etc. are some of the starter projects. It also provides flexibility.
2) Reducing Boilerplate Code
Spring Boot helps to avoid all the manual work of writing boilerplate code,
annotations, and complex XML configurations. it also consumes multiple
Spring Framework’s annotations and provides a single annotation that saves
us from manual configurations including XML based configurations.
3) Embedded Servers
Spring Boot comes with embedded HTTP servers like Tomcat, Jetty and
Undertow. These servers come in the form of jars. However, Tomcat is by
default enabled by Spring Boot.
4) Easier to connect with Databases
It allows connecting easily with databases like Oracle, PostgreSQL, MySQL,
MongoDB, Redis etc.

7) Run Application as a jar file


It provisions to run the jars independently using the command java-jar.
Hence, even we can run a web MVC application using the command java -jar.
8) Spring Boot Profile to easy switch environment
Its profile concept simplifies the process to run our application in different
environments by just changing profile name & server details.

The development process of an application has different stages; the


typical ones are development, testing, and production. Spring Boot profiles
group parts of the application configuration and make it be available only in
certain environments.

A profile is a set of configuration settings. Spring Boot allows to define profile


specific property files in the form of application-{profile}.properties. It
automatically loads the properties in an application.properties file for all
profiles, and the ones in profile-specific property files only for the specified
profile. The keys in the profile-specific property override the ones in the
master property file.
Note: Spring Boot properties are loaded in a particular order. If several
profiles are specified, the last-wins strategy applies.

The @Profile annotation indicates that a component is eligible for registration


when the specified profile or profiles are active. The default profile is called
default; all the beans that do not have a profile set belong to this profile.

There are plenty of ways of defining active profiles in Spring Boot, including
command line arguments, Maven settings, JVM system parameters,
environment variables, spring.profiles.active property,
and SpringApplication methods.
How many ways are there to create a Spring Boot Application?
In order to create a Spring Boot Application, we have multiple ways.

1) Using Spring STS IDE 2) Using Spring Initializr Website

What are the Starters in Spring Boot?


In Spring Boot, Starters are the readymade small projects that we can include
in our application. they are called dependency descriptors. The starters contain
a lot of the dependencies that we need to get a project up and running
quickly and with a consistent, supported set of managed transitive
dependencies.
List of Common Starters

Starter Name Purpose

spring-boot-starter Core starter, including auto-configuration support, logging and YAML

spring-boot-starter- Starter for using Tomcat as the embedded servlet container. Default
tomcat servlet container starter used by starter-web

spring-boot-starter-
data-jpa for using Spring Data JPA with Hibernate

spring-boot-starter-
data-jdbc Starter for using Spring Data JDBC

spring-boot-starter- for building web, including RESTful, applications using Spring MVC.
web Uses Tomcat as the default embedded container

What is The IoC container?


IoC stands for ‘Inversion of Control’. a container that injects dependencies
while creating the bean. The bean itself controlling the instantiation or
location of its dependencies by using direct construction of classes with the
help of the IoC container.

A bean is simply an object that is instantiated, assembled and otherwise


managed by a Spring IoC container. These beans, and the dependencies
between them, are reflected in the configuration metadata used by a container.
Spring Framework’s IoC container. ApplicationContext is a sub-interface
of BeanFactory.
What is an Application Context in Spring Framework?
When you create a project in Spring or Spring Boot, a container or wrapper
gets created to manage your beans.
Spring supports two containers -: BeanFactory and ApplicationContext.
In short, the BeanFactory provides the configuration framework and basic
functionality, and the ApplicationContext adds more enterprise-specific
functionality including easier integration and application-layer specific
contexts such as the WebApplicationContext for use in web applications. The
ApplicationContext is a complete superset of the BeanFactory, Dependency
injection and auto-wiring of beans is done in Application Context.
ApplicationContext represents the Spring IoC container and it manages the
process of instantiating, configuring, and assembling the beans. The container
gets its instructions on what objects to instantiate, configure, and assemble by
reading configuration metadata. The configuration metadata is represented in
XML, Java annotations, or Java code.
@Bean or @Components
During Application start-up, Spring instantiates objects and adds them to
the Application Context. These objects in the Application Context are called
‘Spring Beans’ or ‘Spring Components’. As they are managed by Spring,
therefore we also call them Spring-managed Bean or Spring-managed
Component.

@ComponentScan: is searching packages to visible to ioc container.


The process of searching specific base packages or sub-packages that can
contribute to the Application Context, is called Component Scanning. It will
consider this class as a candidate for Spring Bean/Component and adds it to
the Application Context. Spring explicitly provides a way to identify Spring
bean candidates via the @ComponentScan annotation.
//OR
When to use Component Scanning in a Spring Boot Application?
By default, the @ComponentScan annotation will scan for components in the
current package and all its sub-packages. If it is a Spring Boot application, then
all the packages under the package containing the Main class (a class
annotated with @SpringBootApplication) will be covered by an implicit
component scan.
@Configuration
We apply this annotation on class level. When we apply this to a class, that
class will act as a configuration by itself. It is creating of an object source of
bean methods. Generally, the class annotated with @Configuration has bean
definitions as an alternative to <bean/> tag of an XML configuration. It also
represents a configuration using Java class.
@Configuration
public class AppConfig {
@Bean
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate();
}
}

@Bean
We use @Bean at method level. If you remember the xml configuration of a
Spring, it is a direct analog of the XML <bean/> element. It creates Spring
beans and generally used with @Configuration. A class with @Configuration
(we can call it as a Configuration class) will have methods to instantiate objects
and configure dependencies. Such methods will have @Bean annotation. By
default, the bean name will be the same as the method name. It instantiates
and returns the actual bean. The annotated method produces a bean
managed by the Spring IoC container.
@Configuration
public class AppConfig {
@Bean
public Employee employee() {
return new Employee();
}
@Bean
public Address address() {
return new Address();
}
}

For comparison sake, the configuration above is exactly equivalent to the


following Spring XML:

<beans>
<bean name="employee" class="com.dev.Employee"/>
<bean name="address" class="com.dev.Address"/>
</beans>
The annotation supports most of the attributes offered by <bean/>, such
as: init-method, destroy-method, autowiring, lazy-init, dependency-check, depen
ds-on and scope.
@Component

This is a main stereotype annotation as a class level and interface level. which
indicates that the class is a Spring-managed bean/component. It registers
them into the Application Context as a Spring Bean. When we use this
annotation with class, that class then automatically bean will be created
while using component scan-based annotation configuration. @Controller,
@Service and @Repository are move specific alternatives to @Component.

@Component
class MyBean { }

@Component("myTestBean")
class MyBean { }
@Controller
@Controller annotation comes under the Stereotype category of annotations
that works as specialization of @Component annotation. We use this
annotation with the class, in case of this annotation we need to use
@ResponseBody explicitly. We can used inside the class @RequestMapping
and http request like as @GetMapping, @PostMapping ….
@Controller is a specialized @Component marked as a controller
@RestController-: combination of @Controller +@ResponseBody
@RestController tells Spring Framework that the class annotated with
@RestController will work as a controller in a Spring REST project. It is
combination of @Controller and @ResponseBody get features of REST API.
@Service
@Service tells Spring Framework that the class and interface annotated
with @Service is a part of service layer and it will include business logics of the
application. This annotation behaves similarly to @Component. The only
difference it helps our classes more readable, using this annotation with class
indicates this is our service class (mostly our business logic exists in these
classes).
@Repository
@Repository tells Spring Framework that the class annotated with
@Repository is a part of data access layer and it accessing data from the
database in the application. This annotation used with DAO interfaces.
@Bean vs @Component
@Component is a class level annotation whereas @Bean is a method level
annotation and name of the method serves as the bean name. @Bean
annotation has to be used within the class and that class should be annotated
with @Configuration. However, @Component needs not to be used with
the @Configuration. @Component auto detects and configures the beans using
classpath scanning, whereas @Bean explicitly declares a single bean, rather
than letting Spring do it automatically.

@ComponentScan: is searching packages to visible to ioc container.


The process of searching specific base packages or sub-packages that can
contribute to the Application Context, is called Component Scanning. It will
consider this class as a candidate for Spring Bean/Component and adds it to
the Application Context. Spring container detects Spring managed
components with the help of @ComponentScan. When a Spring application
starts, Spring container needs the information to locate and register all the
Spring components with the application context. However, it can auto scan all
classes annotated with the stereotype annotations such as @Component,
@Controller, @Service, and @Repository from pre-defined project packages.
The @ComponentScan annotation is used with the @Configuration annotation
to ask Spring the packages to scan for annotated components.
@ComponentScan is also used to specify base packages and base package
classes using basePackages or basePackageClasses attributes
of @ComponentScan. For example:
@Configuration
@ComponentScan(basePackages =
{"com.springframework.javatechonline.example.package1",
".package3", ".package4”})
Or
@ComponentScan(basePackages=
com.springframework.javatechonline.example)
public class SpringApplicationComponentScanExample {
....
}
In a Spring Boot project, we typically apply the @SpringBootApplication
annotation on the main application class. Internally, @SpringBootApplication is
a combination of the @Configuration, @ComponentScan, and
@EnableAutoConfiguration annotations. Further, with this default setting, Spring
Boot will auto scan for components in the current package (containing the
SpringBoot main class) and its sub packages.
@Import
Suppose we have multiple Java configuration classes annotated by
@Configuration. @Import imports one or more Java configuration classes.
Moreover, it has the capability to group multiple configuration classes. We use
this annotation where one @Configuration class logically imports the bean
definitions defined by another.

@Configuration
@Import({ DataSourceConfig.class, MyCustomConfig.class })
public class AppConfig extends ConfigurationSupport {
// @Bean methods here that can reference @Bean methods in DataSourceConfig or
MyCustomConfig
}

@Configuration
public class DataSourceConfig {...}

@Component
public class MyCustomConfig {...}
@Value
We can use this annotation to inject values into fields of Spring managed
beans. We can apply it at field or constructor or method parameter level.
let’s first define a property file, then inject values of properties using @Value.
server.port=9898
server.ip= 10.10.10.9
emp.department= HR
columnNames=EmpName,EmpSal,EmpId,EmpDept
Now inject the value of server.ip using @Value as below:
In controller used
@Value("${server.ip}")
private String serverIP;

@SpringBootApplication (@Configuration + @ComponentScan +


@EnableAutoConfiguration)
This annotation applies at the main class which has main () method. The Main
class serves two purposes in a Spring Boot application: configuration and
bootstrapping. In fact @SpringBootApplication is a combination of three
annotations with their default values. They are @Configuration,
@ComponentScan, and @EnableAutoConfiguration.

@EnableAutoConfiguration
@EnableAutoConfiguration enables auto-configuration of beans present in the
classpath in Spring Boot applications. This annotation enables Spring Boot to
auto-configure the application context. Therefore, it automatically creates and
registers beans that are part of the included jar file in the classpath and also
the beans defined by us in the application.
// this annotation automatically configure the spring application based on the
jar dependency that we have added pom.xml file.
we don’t need to explicitly declare a package names using @ComponentScan.

@Configuration
@EnableAutoConfiguration(excludeName =
{"org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessag
ingAutoConfiguration"})
public class MyWebSocketApplication {
public static void main(String[] args) {
}
}
@SpringBootConfiguration
This annotation is a part of Spring Boot Framework. However,
@SpringBootApplication inherits from it. Therefore, if an application uses
@SpringBootApplication, it is already using @SpringBootConfiguration. Moreover,
it’s acts as an alternative to the @Configuration annotation. The primary
difference is that @SpringBootConfiguration allows configuration to be
automatically discovered. @SpringBootConfiguration indicates that the class
provides configuration and also applied at the class level. Particularly, this is
useful in case of unit or integration tests. For example, observe the below code
@SpringBootConfiguration
public class MyApplication { public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public IEmployeeService employeeService() {
return new EmployeeServiceImpl();
}
}

@Configuration
@EnableConfigurationProperties(MyDevAppProperties.class)
public class MySpringBootDevApp { }
Finally create a Test Runner to test the values of properties as
below.
@Component
public class DevPropertiesTest implements CommandLineRunner {

@Autowired
private MyDevAppProperties devProperties;

@Override
public void run(String... args) throws Exception {
System.out.println("App Name = " + devProperties.getName());
System.out.println("DB Url = " + devProperties.getDburl());
System.out.println("DB User = " + devProperties.getDbuser());
}
}

@Autowired
@Autowired facilitates to resolve and inject the object dependency implicitly. It
internally uses setter or constructor injection. It works with reference only. We
can use @Autowired on properties or fields, setters, and constructors.
@Service
public class PaymentService {
public void doPayment() { .... }
}
Then, we’ll inject this bean into the PaymentController bean using
@Autowired on the field definition as below:

@Controller
public class PaymentController {
@Autowired
private PaymentService service;

public void getPayment() {


service.doPayment();
}
}
Here, Spring Container injects PaymentService when PaymentController
created.
@Autowired Execution Precedence
This annotation has execution paths as below listed by precedence:
autowiredByType autowiredByName autowiredByConstructor

@Qualifier
The @Qualifier annotation provides additional information to @ Autowired
annotation while resolving bean dependency. If more than one bean of the
same type is available in the container, we need to tell container explicitly
which bean we want to inject, otherwise the framework will throw
an NoUniqueBeanDefinitionException, indicating that more than one bean is
available for autowiring.
public interface PaymentService { }

@Service
public class CardPaymentService implements PaymentService { }

@Service
public class CashPaymentService implements PaymentService { }

@Controller
public class PaymentController {

@Autowired
@Qualifier("cardPaymentService")
private PaymentService service;
@Autowired
@Qualifier("cashPaymentService")
private PaymentService service;

public void getPayment() {


service.doPayment();
}
}

Here, by including the @Qualifier annotation avoid ambiguity as Spring


container finds two beans of the same type.
@Primary vs @Qualifier
We can also use @Primary annotation when we need to decide which bean to
inject in case of ambiguity. Similar to @Qualifier. In that situation, we use
@Primary in the sense of a default, while @Qualifier in the sense of a very
specific.
Spring Boot MVC Annotations
In order to use Spring Boot MVC annotations, make sure that you have the
below dependency in your pom.xml.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@Controller
@Controller annotation comes under the Stereotype category of annotations
that works as specialization of @Component annotation. This annotation tells
the Spring IOC container to treat this class just as a Spring MVC controller. We
can used inside the class @RequestMapping and http request like as @GetM
apping, @PostMapping …..

@Controller
public class MyMvcController { }
@RequestMapping
We use this annotation to map client requests with the appropriate method to
serve the request. Apart from Spring MVC, Spring WebFlux also supports this
annotation. @RequestMapping annotation provides various options as its
attributes to offer its customized behaviour. Below is the list of attributes it
provides.
value -: represents the primary mapping which becomes a part of our URL.
Most of the times, for a simple request, we use only this attribute.
path -: Alias for value, supports Ant-style path patterns, relative paths at
method level. Path mapping URIs may contain placeholders e.g. “/${profile
path}”
method : represents the HTTP request method. However, now-a-days we
use @GetMapping, @PostMapping annotations as a replacement of this
attribute.
params : represents the parameters of the mapped request, yet another way of
narrowing the request mapping.
consumes : represents the consumable media types of the mapped request.
headers : represents the headers of the mapped request.
name : assigns a name to this mapping.
produces : represents the producible media types of the mapped request.
@Controller
public class HelloController {

@RequestMapping(
value = {"/hello"},
params = {"id","name"},
method = {RequestMethod.GET},
consumes = {"text/plain"},
produces = {"application/json","application/xml"},
headers = {"name=Robert", "id=1"}
)
public String helloWorld() {
return "Hello";
}
}
We can use this annotation at both class level and method level. we have more
handy modern annotations(available in Spring 4.3+) to use at method level
such as @GetMapping, @PostMapping etc.
@GetMapping, @PostMapping, @PutMapping, @PatchMapping,
@DeleteMapping
@GetMapping annotation is the HTTP method ‘GET’ specific variant of the
annotation @RequestMapping. It is a shortcut version of the ‘@RequestMapping‘
and applies on top of the method that expects HTTP ‘GET’ request. Let’s look
once at the API source code of annotation @GetMapping.
@Target({ java.lang.annotation.ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = { RequestMethod.GET })
public @interface GetMapping {
String name( ) default "";
String[ ] value() default {};
String[ ] path() default {};
String[ ] params() default {};
String[ ] headers() default {};
String[ ] consumes() default {};
String[ ] produces() default {};
}

@GetMapping vs @RequestMapping
As source code of @GetMapping shows that it is already annotated
with @RequestMapping(method = { RequestMethod.GET }). It indicates that we
don’t need to specify ‘method = { RequestMethod.GET }‘when
using @GetMapping. For example, below code demonstrates the concept:
@RequestMapping(value = "/emp/{empid}", method =
RequestMethod.GET)
is equivalent to ⇓

@GetMapping("/emp/{empid}")
Similarly, to handle different types of incoming HTTP requests, Spring supports
five types of HTTP method specific annotations. They are @GetMapping,
@PostMapping, @PutMapping, @PatchMapping, @DeleteMapping for HTTP
methods GET, POST, PUT, PATCH and DELETE respectively.

♥ Note : In Spring MVC applications, we generally use @GetMapping and


@PostMapping. Including both of them are useful in Spring REST applications.
@ModelAttribute

In Spring MVC, you will definitely have a form to fill the values and submit it
on click of a provided button. Instead of parsing each field value of your form
individually, you can populate all fields value at one go with the help of
@ModelAttribute annotation. This is commonly known as data binding in
Spring MVC. For example, the code below that follows the ‘Invoice‘ model
attribute is populated with data from a form submitted to the saveInvoice
endpoint. Spring MVC does this behind the scenes before invoking the submit
method:
@ModelAttribute at method argument level
@PostMapping("/saveInvoice")
public String saveInvoice(@ModelAttribute("invoice") Invoice invoice) {
// Code that uses the invoice object to save it
// service.saveInvoice(invoice);
return "invoiceListView";
}
@ModelAttribute at method level
Moreover, besides using @ModelAttribute at method argument, we can also
use it at method level. When used at the method level, it adds one or more
model attribute values in the Model that can be identified globally. It means,
methods annotated with @ModelAttribute apply to all methods that are
annotated with @RequestMapping or even @GetMapping and @PostMapping.
However, our method annotated with @ModelAttribute will be the very first to
run, before the rest of the @RequestMapping methods.
@ModelAttribute
public void includeAttributes(Model model) {
model.addAttribute("message", "additional attribute to all methods");
}
In the example above, includeAttributes() method will add an attribute named
‘message’ to all models defined in the controller class. Generally, Spring MVC
will always make first call to this method, prior to calling any other request
handler methods.

♥ Note: It is also important to note that you need to annotate the respective
controller class with @ControllerAdvice. Then only, you can add values in the
Model that will be identified globally(in all request handler methods).
@RequestParam
@RequestParam annotation binds the value of web request parameter with the
value of Controller’s method parameter. It acts just like getRequestParameter()
method of HttpServletRequest. We send data to application using URL in the
form of /user?uid=5&uname=Robin.
For example, below code demonstrates the concept:

Expected URL to access this method :


@GetMapping("/user")
public String getUserDetails(
@RequestParam("uid") int id,
@RequestParam(value = "uname", required = false, defaultValue
= "Mary") String name)
{
System.out.println("id is : "+id);
}
example above, it should be clear that how we can use multiple attributes also.

@RequestParam with multiple values of a field


Sometimes, it is recommended to declare variable as Array or List types. In
such a case we need to supply multiple values to @RequestParam. Of course,
we can compare it with getParametervalues() method of HttpServletRequest.
Here is the example to assign multiple values to a field:

Expected URL to access this method : …./user?


subject=IT&subject=CS&subject=EC
@RequestParam("subject")String[] sub
--OR--
@RequestParam("subject")List<String> sub
However, internally it will act as String[] sub = {“IT”, “CS”, “EC”};

Spring Boot REST Annotations


This section of our article ‘Spring Boot MVC REST Annotations with Examples’
is dedicated to Spring Boot REST annotations. All annotations which offers
support for Spring MVC web applications are also applicable for Spring REST
application. Visit our article ‘How to create CRUD REST API using Spring Boot ?.
@Controller
@Controller annotation comes under the Stereotype category of annotations
that works as specialization of @Component annotation. We use this
annotation with the class, in case of this annotation we need to use
@ResponseBody explicitly. We can used inside the class @RequestMapping
and http request like as @GetMapping, @PostMapping …..
@Controller is a specialized @Component marked as a controller
@RestController -: combination of @Controller +@ResponseBody
@RestController used in restful web services & it is combination of @Controller
+@ResponseBody annotation. This class indicate as controller where @
RequestMapping method assume @ResponseBody by defaults. It is specialization of
@controller annotation.

@RestController
@RequestMapping("/user")
public class UserRestController {

@GetMapping("/getUser/{uid}")
public User getUser(@PathVariable String uid) {....}
}
The code above indicates that the class UserRestController will act as a
RestController for the application.

@PathVariable
We use @PathVariable to bind value of variable at URL path with request
handler’s method parameter. In @RequestParam, we were sending data via URL
with query string (?) and then ‘key=value’. In @PathVariable also, we can send
multiple data separated by ‘/’ but without using ‘?’ and key-value. In order to
read the dynamic value, variable in the URL path should be enclosed with curly
braces such as “URL/user/{id}“.
For example, below code demonstrates the use of @PathVariable:
@GetMapping("/user/{id}/{name}")
public String getUserDetails ( @PathVariable Integer id,
@PathVariable String name) {
return "Path variable data is: " + id + "-" + name;
}
In order to access getUserDetails(), your URL should be like
http://localhost:8080/user/10/Robin
In Spring, method parameters annotated with @PathVariable are required by
default. However, to make it optional, we can set the required property
of @PathVariable to false as below:
@PathVariable(required = false) String name
Moreover, Since Spring 4.1, we can also use java.util.Optional<T> (introduced in
Java 8) to handle a non-mandatory path variable as below:
@GetMapping("/user/{id}/{name}")
public String getUserByIdAndName(@PathVariable
Optional<String> id,
@PathVariable String name){
if (id.isPresent()) { return "ID: " + id.get();}
else {return "ID not present";}
}
@RequestParam vs @PathVariable
We generally use @RequestParam in form-based Spring MVC projects
whereas @PathVariables in Spring REST applications. Almost all Web
technologies in Java supports @RequestParam as it is a basic concept of
Servlet API. But it is not true with @PathVariable. One major difference is
that @PathVarible follows the order of variables whereas
in @RequestParam order of variables doesn’t matter.
URL pattern Validity

/user?id=10&name=Robin Valid

/user?name=Robin&id=10 Valid

/user/10/Robin Valid

/user/Robin/10 Invalid :
MethodArgumentTypeMismatchException(NumberFormatException)
with "status": 400, "error": "Bad Request"

From the table above, it is clear that we have to take extra care while accessing
a request handler method in terms of variable order in @PathVariable.
As opposed to @RequestParam, URL created for @PathVariable is called as Clean
URL and it also takes less characters to send data.
@ResponseBody
In Spring Boot, @ResponseBody, by default, converts return type data into
JSON format in case of non-string return type data(e.g. Employee,
Employee<String> etc.). Hence, if return type of request handler method is
String, @Responsebody will not do any conversion of returning data. We don’t
need to apply this annotation explicitly as Spring will internally apply it by
default when we annotate a class with @RestController.
@RequestBody
If we are sending input data in form of JSON/XML(Global data format) to a
Spring Boot REST producer application via request handler method, then it will
convert Global data into Object format(based on the provided type) and pass
it to method argument. This annotation converts input data from JSON/XML
format into Object. We provide this object as method parameter. In Spring
Boot application, @RequestBody annotation does all this behind the scene.
Below is the syntax to use @RequestBody:
@RequestBody ClassName objectName
For example, below code demonstrates the use of @RequestBody

@RestController
public class EmployeeRestController {

@PostMapping("/save")
public String saveEmp(@RequestBody Employee employee) {
return employee.toString();
}
}
Moreover, if we send invaild JSON/XML, like format is wrong, key-val are
wrong, then spring boot throws : 400 BAD REQUEST.

@ResponseBody & @RequestBody for XML format data


As aforementioned Spring Boot by default works for JSON format data. To
work with XML format data, add below dependency and provide header
param as Accept : application/xml

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-datafornmat-xml</artifactId>
</dependency>
If Producer REST application has no dependency for XML, still Request Header
has Accept : application/xml, then you will get Http Status: 406 Not
Acceptable.

If above XML dependency is not in place and trying to accept XML data
using @RequestBody, then you will get Http Status: 415 Unsupported
MediaTypes.
Generally, Http Status Codes come under below five categories.

Informational responses (100–199)


Successful responses (200–299)
Redirects (300–399)
Client errors (400–499) : denotes Errors in Java
Server errors (500–599) : denotes Exceptions in Java
What are some commonly used Http Response Status codes?
OK : 200
CREATED : 201
NO_CONTENT : 204
MULTIPLE_CHOICES : 300
NOT_MODIFIED : 304
BAD_REQUEST : 400
UNAUTHORIZED : 401
PAYMENT_REQUIRED : 402
FORBIDDEN : 403
NOT_FOUND : 404
REQUEST_TIMEOUT : 408
INTERNAL_SERVER_ERROR : 500
NOT_IMPLEMENTED : 501
BAD_GATEWAY : 502
GATEWAY_TIMEOUT : 504
HTTP_VERSION_NOT_SUPPORTED : 505
XML: Extensible Markup Language
JSON: JavaScript Object Notation
Both are self-describing (human readable) hierarchical (values within value) data
which is used to describe an object.

Similarities:
self-describing (human readable) hierarchical (values within value)
can be parsed and used by lots of programming languages
can be fetched with XMLHttpRequest
Difference:
JSON doesn’t used end tag JSON is shorter
JSON is quicker to read write JSON can be used Arrays
Difference between Spring Frameworks vs Spring Boot Framework.

Why REST

Spring web Services follow to SOAP Spring web follow REST APIs in MVC
I would handle the exceptions with Spring’s @ControllerAdvice and
@ExceptionHandler. With this, I can send the status code and error
message in the response entity.
@RestController
Public class EmployeeController{
Public List <Employee> asList=new ArrayList();
@GetMapping(“/{id}”)
Employee getById(@PathVariable Long id){
Return asList.stream().filter(e ->
e.id=id).findFirst().orElseThrow(EmployeeNotFoundException::new );
}
}
public class EmployeeNotFoundException extends RuntimeException{
EmployeeNotFoundException(){
super(“Employee not found ”, HttpStatus.NOT_FOUND);
}
}
Any class annotated with @ControllerAdvice will become a controller-
advice class which will be responsible for handling exceptions. Under
this class, we make use of annotations provided as @ExceptionHandler,
@ModelAttribute, @InitBinder.

Spring is a popular Java application framework and Spring Boot is an


evolution of Spring that helps create stand-alone, production-grade Spring
based applications easily.

Spring MVC

Spring MVC is the primary web framework built on the Servlet API. It is build
on the popular MVC design pattern. MVC (Model-View-Controller) is a
software architecture pattern, which separates application into three areas:
model, view, and controller. The model represents a Java object carrying data.
The view represents the visualization of the data that the model contains. The
controller controls the data flow into model object and updates the view when
the data changes. It separates the view and model.

Spring Framework 5.0 introduced a parallel reactive stack web framework


called Spring WebFlux.

@RestController

@RestController is a convenience annotation for creating Restful


controllers. It is a specialization of @Component and is autodetected through
classpath scanning. It adds
the @Controller and @ResponseBody annotations. It converts the response
to JSON or XML. It does not work with the view technology, so the methods
cannot return ModelAndView. It is typically used in combination with
annotated handler methods based on the @RequestMapping annotation.

The ResponseEntity class represents an HTTP response, including headers,


body, and status. It is used to return data.

The @Controller annotation is used with the view technology.

Restful application

A RESTFul application follows the REST architectural style, which is used for
designing networked applications. RESTful applications generate HTTP
requests performing CRUD (Create/Read/Update/Delete) operations on
resources. RESTFul applications typically return data in JSON or XML format.

spring boot actuator:


Spring Boot Actuator includes a number of additional features to help you
monitor and manage our application when it’s pushed to production. We can
choose to manage and monitor our application using HTTP request. Auditing,
health, and metrics gathering can be automatically applied to our application.
we need to add the Spring Boot Starter actuator dependency in our build configuration
file.
For maven we can used dependency in pom.xml
Spring boot starter actuator
We can also use separate port for accessing spring actuator in application.properties
file management:port:9090
Endpoints: Actuator endpoints allow you to monitor and interact with your
application. Spring Boot includes a number of built-in endpoints.
For example, the health endpoint provides basic application health
information. Run up a basic application and look at /actuator/health.

You might also like