spring boot: use because of --> if we wan to develop an end to end sys like controller service or from scratch we can use spring boot
. spring boot feature that provide when create an API or application
. spring boot is==== spring is frame for entrprice java applicationn ,, boot stands for is bootstrap a spring application ,,it make easy to create stand alone ,production -grade spring based application that you can "just run"
--stand alone
--production grade
. Spring is dependency injunction in older definition ,, now it is more..... lot of things in spring ,,
. spring has infrastructure suport to connect with db like casandra,mongo
. spring have not of configuration,in different ways
.spring boot ---
its stand alone , what u get u can start run ,and don't want start web server don't want to search for servlet ,and also is production ready
.--->convert simple maven to spring boot project
we using a <parent> tag with spring parameter to show the app is child of this parent spring
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
this parent project above showing
spring boot team create a project "spring-boot-starter-parent" above spring boot team created a project called spring-boot-starter-parent ,which contains opinionated set of maven configuration
and it provide set of maven depetency into pom.xml for creating the project.
then we add "spring-boot-starter-jdbc" , "spring-boot-starter-web" which download the jar which mete dependency (these are articact id in dependency)
-->there is <properties> tag in pom.xml ,here which is used for use latest version of jdk
<properties>
<java.version>1.8</java.version>
</properties>
-->in spring boot application we have a class with public static void main because spring boot is stand alone son need a class which bootstrap the app,,so don not need diployee on server or servelt container
main methord start the app, it create the servelet container and it do everything
--> steps in class which has main method
1.we need to tell this is the spring application for that use @SpringBootApplication this tell this is the starting position
2.the we need to tell,spring boot to start this application and create servelet container and host this application on that servelt container ,this step done in one line
is SpringAppliction.run(CourseApi.class,args); ----->courseApi is calls which contain the main method
@SpringBootApplication
public class CourseApi {
//this class we are going for boot strap the application
public static void main(String[] args) {
SpringApplication.run(CourseApi.class, args);
}
}
spring is container that contain controller,business services,data services spring has container that contain all those services this is called application context of the application
each logics are implemented in classes with corresponding annotation like service classes annotated with @service and data access annotated with @Repository and son on.
spring want to look these classes and distinguish these classes according to annotation.so we need to scan the class path. so need give component scan.
#####
->@SpringBootApplication(scanBasePackages= {"com.leejo.controller"})
-->Here we need controller to handle the request,@RestController ->it means we can map to request to methord of class which use this anottation
-->@ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.
@ResponseBody is a marker for the HTTP response body and @ResponseStatus declares the status code of the HTTP response.
@ResponseEntity in detail===> we can many things with responseentity
@RequestMapping("/handle")
public ResponseEntity<String> handle() {
URI location = ...;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(location);
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
--->RequestEntity and ResponseEntity
@RequestMapping("getTopicsRequest")
public ResponseEntity<List<Topics>> getTopicsRequest(RequestEntity<List<Topics>> requestEntity){
System.out.println("request body : " + requestEntity.getBody());
HttpHeaders headers = requestEntity.getHeaders();
System.out.println("request headers : " + headers);
HttpMethod method = requestEntity.getMethod();
System.out.println("request method : " + method);
System.out.println("request url: " + requestEntity.getUrl());
return new ResponseEntity<>(null,HttpStatus.OK);
}
--->@RequestBody sample this can use viseversa with RequestEntity
@RequestMapping("getTopicsRequestBody")
public ResponseEntity<List<Topics>> getTopicsRequestBody(@RequestBody List<Topics> topics){
System.out.println("request body : " + topics);
return new ResponseEntity<>(null,HttpStatus.OK);
}
if you want to show the URI as requestentity above exapmle we have to use "UriComponentsBuilder" in parameter list
@PostMapping("book")
public ResponseEntity<Void> addBook(@RequestBody Book book,UriComponentsBuilder builder){
int flag=bookServiceImpl.addBook(book);
if(flag==0) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/book/{book_id}").buildAndExpand(book.getBook_id()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}