Spring Boot
Spring Boot
Web-Services
1. A service which provides a data of particular application is known as webservice
2. Webservice will not having user interface but they have ability to communicate with other project or
frameworks and share the information
3. Webservice are also web applications
4. But all web application is not webservice
5. To build webservice using spring boot we use REST architecture
6. Any application which has been build using REST architecture is known as RESTful webservice or web
applications
7. Since webservice provides service to the various clients they have to share the data in universal language
or the language which can be understandable client that is web services shares the data in JSON or XML
format
} }
} }
class Car{ {
String brand; “brand”: ”TATA”,
} “eid”:101,
int eid; },
int cc; }
List or Collection {
class Student{ “name”: ”Anis”,
} “name”:”JAVA”,
int sid; ],
String name; }
@SpringBootApplication
@SpringBootApplication it is a driver class from which application execution will start
In spring boot maven project, we can have @SpringBootApplication only once
@SpringBootApplication will do auto configuration and @ComponentScan internally
Note:
1. in controller we can have multiple api’s with same url but mapping annotation should be different
2. in controller we can have multiple api’s with same maping annotations but url should be different
==========================================================================================
reading a data from the request inside rest api using following annotation
1. @RequestParam:
a. @RequestParam is used to read the data from the request which is passed as QueryString or
Query Parameter
2. @PathVariable:
a. Whenever we want to read data from the request which is passed through the url itself then we
make use of @PathVariable annotation
b. If we are passing a data through the url it is recommended to pass data name first and then the
actual data in url
3. @RequestHeader:
a. Whenever we want to read header data of http request, we make use of @RequestHeader
b. In header of request the data will be stored in key-value pair
c. To read that data using @RequestHeader we need to declare variable with the name same as
that of key
4. @CookieValue:
a. @CookieValue Is used to read the cookie which is coming to the request
b. The variable declared with @CookieValue annotation and the cookie key should be same
5. @RequestBody:
a. Whenever we want to create json object into java object present inside spring boot application
we make use of @RequestBody
b. @RequestBody it will be used with class reference variable
==========================================================================================
MediaType
Rest apis to consume or produce a data in particular format that is either in XML or JSON we make use of
MediaType
If we want to configure media type to consume the data we use consumes attribute in mapping annotation
If we want our api to produce the data in particular data format we use produces attribute in mapping
annotation
if we don’t mention any mediatype in spring boot bydefault it will consider it as JSON
if we want to make mediatype as XML then we need to add Jackson dataformat XML as dependency as a
mediatype convertor and use XML mediatype
https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml
in spring boot application, we need to design apis to take request to produce responses
to redesign the response, we make use of ResponseStructure and ResponseEntity because we need to provide
status code, data, message etc. and give it as a response based on this response client will consume and
display the proper message and data on the browser.