Spring Boot Notes Lavish
Spring Boot Notes Lavish
Spring IOC container will take care of creating beans.We don't have to create object
package com.codingmaster.springannotations.controller;
import org.springframework.stereotype.Component;
@Component
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
PizzaController pizzaController=context.getBean(PizzaController.class);
System.out.println(pizzaController.getPizza());
Output:
Hot Pizza
PizzaControlle.java
package com.codingmaster.springannotations.controller;
import org.springframework.stereotype.Component;
@Component("demopizza")
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
System.out.println(pizzaController.getPizza());
package com.codingmaster.springannotations.controller;
import org.springframework.stereotype.Component;
@Component
}
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
System.out.println(pizzaController.getPizza());
}
Conclusion:Here we are using @Component Annotation to create spring bean automatically in Spring
container. We don't
have to create object using new keyword.Spring IOC container takes care of managing object for us.
2)
@Autowired ->
used in->
VegPizza.java
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
public class VegPizza {
Step-2
Inject->veg pizza in Pizza Controller and get the data using annotation
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
}
PizzaController.java
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Autowired
this.vegPizza=vegPizza;
return vegPizza.getPizza();
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
System.out.println(pizzaController.getPizza());
Output:
veg Pizza
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
@Component
// Using Constructor
// @Autowired
// this.vegPizza=vegPizza;
// }
@Autowired
this.vegPizza = vegPizza;
return vegPizza.getPizza();
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
System.out.println(pizzaController.getPizza());
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Step-1
@Component
@Autowired
// Using Constructor
@Autowired
this.vegPizza=vegPizza;
@Autowired
this.vegPizza = vegPizza;
return vegPizza.getPizza();
Step-2
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
Step-3
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
System.out.println(pizzaController.getPizza());
Output->Veg pizza
3)
@Qualifier Annotation->
Pizza.java(I)
Pizza.java(I)
package com.codingmaster.springannotations.service;
String getPizza();
2)
VegPizza(C)
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
@Override
}
}
3)
NonVegPizza(C)
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
@Override
controller-PizzaController
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
// Using Constructor
@Autowired
this.pizza=pizza;
return pizza.getPizza();
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
// Using Constructor
@Autowired
this.pizza=pizza;
return pizza.getPizza();
Output:Non-Veg pizza
or
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
// Using Constructor
@Autowired
this.pizza=pizza;
return pizza.getPizza();
Veg Pizza
annotation
Final COde:
Pizza Interface
package com.codingmaster.springannotations.service;
VegPizza class
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
public class VegPizza implements Pizza {
@Override
public String getPizza() {
return "Veg Pizza";
}
}
NonVegPizza class:
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Component;
@Component
public class NonVegPizza implements Pizza{
@Override
public String getPizza() {
return "Non-Veg pizza";
}
}
4)
Controllerpackage->PizzaController class
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class PizzaController {
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
System.out.println(pizzaController.getPizza());
}
4)
4.@Primary Annotation:
We use @Primary annotation to give higher preference to bean when there are multiple beans of
In vegpizza or nonvegpizza class, add @primary annotation ->Add in one of them not for both.
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class PizzaController {
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class NonVegPizza implements Pizza{
@Override
public String getPizza() {
return "Non-Veg pizza";
}
}
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
//@Primary
public class VegPizza implements Pizza {
@Override
public String getPizza() {
return "Veg Pizza";
}
}
Run main :
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
public static void main(String[] args) {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
System.out.println(pizzaController.getPizza());
Output
. ____ _ __ _ _
=========|_|==============|___/=/_/_/_/
Non-Veg pizza
@Bean Annotation->
Definitions.
AppConfig->
object using new keyword and telling Spring Container to manage the object
Code:
Config- package->Appconfig.java
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public Pizza vegPizza(){
return new VegPizza();
}
}
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Override
public String getPizza() {
return "Veg Pizza";
}
}
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
//@Component
//@Primary
public class NonVegPizza implements Pizza{
@Override
public String getPizza() {
return "Non-Veg pizza";
}
}
package com.codingmaster.springannotations.service;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
// PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
// System.out.println(pizzaController.getPizza());
VegPizza vegPizza=context.getBean(VegPizza.class);
System.out.println(vegPizza.getPizza());
NonVegPizza nonvegPizza=context.getBean(NonVegPizza.class);
System.out.println(nonvegPizza.getPizza());
Code:2
Service package:
package com.codingmaster.springannotations.service;
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
//@Component
//@Primary
public class NonVegPizza implements Pizza{
@Override
public String getPizza() {
return "Non-Veg pizza";
}
}
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {
@Bean
public Pizza vegPizza(){
return new VegPizza();
}
@Bean
@Primary
public Pizza nonvegPizza(){
return new NonVegPizza();
}
}
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
// PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
// System.out.println(pizzaController.getPizza());
VegPizza vegPizza=context.getBean(VegPizza.class);
System.out.println(vegPizza.getPizza());
NonVegPizza nonvegPizza=context.getBean(NonVegPizza.class);
System.out.println(nonvegPizza.getPizza());
Output:
Veg Pizza
Non-Veg pizza
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {
@Bean(name = "vegpizzabean")
public Pizza vegPizza(){
return new VegPizza();
}
@Bean(name = "nonvegpizzabean")
@Primary
public Pizza nonvegPizza(){
return new NonVegPizza();
}
}
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
// PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
// System.out.println(pizzaController.getPizza());
// VegPizza vegPizza=context.getBean(VegPizza.class);
VegPizza vegPizza= (VegPizza) context.getBean("vegpizzabean");//access
using bean name
System.out.println(vegPizza.getPizza());
// NonVegPizza nonvegPizza=context.getBean(NonVegPizza.class);
NonVegPizza nonvegPizza= (NonVegPizza)
context.getBean("nonvegpizzabean");
System.out.println(nonvegPizza.getPizza());
}
Bydefault ->we can access by using method name without wiring any name attribute in @Bean
annotation in spring
Conclusing->
Config->AppConfig
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {
@Bean(name = "vegpizzabean")
public Pizza vegPizza(){
return new VegPizza();
}
@Bean(name = "nonvegpizzabean")
@Primary
public Pizza nonvegPizza(){
return new NonVegPizza();
}
@Bean
public PizzaController pizzaController(){
return new PizzaController(vegPizza());//call object of vegPizza class
}
}
controller-PizzaController:
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
service:
package com.codingmaster.springannotations.service;
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
package com.codingmaster.springannotations.service;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
public class VegPizza implements Pizza {
@Override
public String getPizza() {
return "Veg Pizza";
}
}
Spring main :
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
System.out.println(pizzaController.getPizza());
// VegPizza vegPizza=context.getBean(VegPizza.class);
// VegPizza vegPizza= (VegPizza) context.getBean("vegpizzabean");//access
using bean name
// System.out.println(vegPizza.getPizza());
// NonVegPizza nonvegPizza=context.getBean(NonVegPizza.class);
// NonVegPizza nonvegPizza= (NonVegPizza)
context.getBean("nonvegpizzabean");
// System.out.println(nonvegPizza.getPizza());
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {
@Bean(name = "vegpizzabean")
public Pizza vegPizza(){
return new VegPizza();
}
@Bean(name = "nonvegpizzabean")
@Primary
public Pizza nonvegPizza(){
@Bean
public PizzaController pizzaController(){
@Bean(name="beanname")
package com.codingmaster.springannotations.config;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {
@Bean(name = "vegpizzabean")
public Pizza vegPizza(){
return new VegPizza();
}
@Bean(name = "nonvegpizzabean")
@Primary
public Pizza nonvegPizza(){
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.service.Pizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
In summary, while not using @Component gives you more control over how
your classes are managed by Spring, it also requires more manual
configuration and can be less convenient, especially for smaller or simpler
applications. @Component provides a convenient way to let Spring handle the
management of your classes, reducing boilerplate code and making your
application more maintainable.
1.@Controller,@Repository,@Service
These annotations are used to categorize Spring-managed components based on their roles
in
the application. @Controller is used to mark classes as controllers for handling web
requests,
@Service is used for business logic components, and @Repository is used for data access
components.
Project Structure-main
MyController.java
package com.codingmaster.springannotations.controller;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
public String hello(){
return "hello controller.";
}
}
MyRepository.java
package com.codingmaster.springannotations.repository;
import org.springframework.stereotype.Repository;
@Repository
public class MyRepository {
public String hello(){
return "hello repository";
}
}
MyService.java
package com.codingmaster.springannotations.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String hello(){
return "hello service";
}
}
@Lazy Annotation:
@Lazy Annotation can be used
@Configuration
@Component
@Bean Annotation
Implementation:
package com.codingmaster.springannotations.lazy;
import org.springframework.stereotype.Component;
@Component
public class EagerLoader {
public EagerLoader() {
System.out.println("EagerLoader....");
}
}
package com.codingmaster.springannotations.lazy;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy
public class LazyLoader {
public LazyLoader() {
System.out.println("LazyLoader....");
}
}
package com.codingmaster.springannotations;
import com.codingmaster.springannotations.controller.MyController;
import com.codingmaster.springannotations.controller.PizzaController;
import com.codingmaster.springannotations.lazy.LazyLoader;
import com.codingmaster.springannotations.repository.MyRepository;
import com.codingmaster.springannotations.service.MyService;
import com.codingmaster.springannotations.service.NonVegPizza;
import com.codingmaster.springannotations.service.VegPizza;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringannotationsApplication {
var context=SpringApplication.run(SpringannotationsApplication.class,
args);
// PizzaController pizzaController= (PizzaController)
context.getBean("pizzaController");
// System.out.println(pizzaController.getPizza());
MyController myController = context.getBean(MyController.class);
System.out.println(myController.hello());
MyService myService = context.getBean(MyService.class);
System.out.println(myService.hello());
MyRepository myRepository = context.getBean(MyRepository.class);
System.out.println(myRepository.hello());
// VegPizza vegPizza=context.getBean(VegPizza.class);
// VegPizza vegPizza= (VegPizza) context.getBean("vegpizzabean");//access
using bean name
// System.out.println(vegPizza.getPizza());
// NonVegPizza nonvegPizza=context.getBean(NonVegPizza.class);
// NonVegPizza nonvegPizza= (NonVegPizza)
context.getBean("nonvegpizzabean");
// System.out.println(nonvegPizza.getPizza());
}
@Controller Annotation:
package com.codingmaster.springannotations.bean;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
'}';
}
}
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BookController {
@RequestMapping("/hello-world")
@ResponseBody
public String hello(){
return "Hello WOrld";
}
@RequestMapping("/book")
@ResponseBody
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
Whenever we annotate class @Controller, it become Spring MVC controller automatically Scan that
class and create bean for that class.
Inside controller, we create handle method and map them to send and receive request-
>@RequestBody in form of json format and write json into and return http response back to client.
import com.codingmaster.springannotations.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@RequestMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
Whenever ,you want to develop Restful webservices using Spring MVC ,then use
@RestController Annotation.
@RequestMapping Annotation:
How to use @RequestMapping annotation at class level?
@RequestMapping(“/api”) in class-
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@RequestMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@RequestMapping(value={"/book","/corejava","/java"})
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
http://localhost:8080/api/book
http://localhost:8080/api/corejava
http://localhost:8080/api/java
Output:
// 20240316202559
// http://localhost:8080/api/java
{
"id": 1,
Bydefault-Get
methodAttribute to be used
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@RequestMapping(value={"/book","/corejava","/java"},
method= RequestMethod.GET
)
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
@RequestMapping with Produces and consumes:
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@RequestMapping(value={"/book","/corejava","/java"},
method= RequestMethod.GET,
produces =
{MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE},
consumes =
{MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE}
)
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
@GetMapping Annotation:
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@GetMapping(value={"/book","/corejava","/java"})
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
For one end point:
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@GetMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
}
2.@PostMapping annotation:
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@GetMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
@PostMapping(value="/books/create",
consumes = MediaType.APPLICATION_JSON_VALUE)
public Book createBook(@RequestBody Book book){
System.out.println(book.getId());
System.out.println(book.getTitle());
System.out.println(book.getDescription());
return book;
}
}
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@GetMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
@PostMapping(value="/books/create",
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value= HttpStatus.CREATED)
public Book createBook(@RequestBody Book book){
System.out.println(book.getId());
System.out.println(book.getTitle());
System.out.println(book.getDescription());
return book;
}
}
Http status using Response Entity class :
package com.codingmaster.springannotations.controller;
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/hello-world")
public String hello(){
return "Hello WOrld";
}
@GetMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
// @PostMapping(value="/books/create",
// consumes = MediaType.APPLICATION_JSON_VALUE)
// @ResponseStatus(value= HttpStatus.CREATED)
// public Book createBook(@RequestBody Book book){
// System.out.println(book.getId());
// System.out.println(book.getTitle());
// System.out.println(book.getDescription());
// return book;
// }
@PostMapping(value="/books/create",
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Book> createBook(@RequestBody Book book){
System.out.println(book.getId());
System.out.println(book.getTitle());
System.out.println(book.getDescription());
return new ResponseEntity<>(book,HttpStatus.CREATED);
}
}
@PutMapping annotation:
import com.codingmaster.springannotations.bean.Book;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@GetMapping("/book")
public Book getBook(){
Book book=new Book(1,"Core Java","James Gosling");
return book;
}
// @PostMapping(value="/books/create",
// consumes = MediaType.APPLICATION_JSON_VALUE)
// @ResponseStatus(value= HttpStatus.CREATED)
// public Book createBook(@RequestBody Book book){
// System.out.println(book.getId());
// System.out.println(book.getTitle());
// System.out.println(book.getDescription());
// return book;
// }
// {
// "id":101,
// "title":"Python",
// "description":"Guido van rossum"
// }
// http://localhost:8080/api/books/create
@PostMapping(value="/books/create",
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Book> createBook(@RequestBody Book book){
System.out.println(book.getId());
System.out.println(book.getTitle());
System.out.println(book.getDescription());
return new ResponseEntity<>(book,HttpStatus.CREATED);
}
//update
// Use @pathannotation->to bind the value of url path variable to method
argument
// @Requestbody->to retrieve json from request and convert json into java
book object
@PutMapping(value="/books/update/{id}")
public ResponseEntity<Book> updateBook(@PathVariable int id, @RequestBody
Book updatedBook){
System.out.println(id);
System.out.println(updatedBook.getTitle());
System.out.println(updatedBook.getDescription());
updatedBook.setId(id);
return ResponseEntity.ok(updatedBook);
}