Task - Spring Boot - FooBar App #59
akash-coded
started this conversation in
Tasks
Replies: 6 comments
ComponentsFoo.javapackage com.spring.boot.foobar.demo.beans;
import org.springframework.stereotype.Component;
@Component
public class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Foo(String name) {
this.name = name;
}
public Foo() {
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Bar.javapackage com.spring.boot.foobar.demo.beans;
import org.springframework.stereotype.Component;
@Component
public class Bar {
private String name;
private int age;
private Foo foo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Bar() {
super();
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + "]";
}
}Bean Definition classpackage com.spring.boot.foobar.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.boot.foobar.demo.beans.Bar;
import com.spring.boot.foobar.demo.beans.Foo;
@Configuration
public class FooBarConfiguration {
@Bean
public Foo getFoo() {
return new Foo("Goutham");
}
@Bean
public Bar getBar() {
Bar bar = new Bar("Gk", 29);
bar.setFoo(getFoo());
return bar;
}
}ControllersHome Controllerpackage com.spring.boot.foobar.demo.controller;
import java.sql.Timestamp;
@RestController
public class HomeController {
@GetMapping
public String welcomeVisitor() {
return "Hello visitor!\\nVisiting time: "
+ new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp(System.currentTimeMillis()));
}
}FooBarControllerpackage com.spring.boot.foobar.demo.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spring.boot.foobar.demo.beans.Bar;
import com.spring.boot.foobar.demo.beans.Foo;
import com.spring.boot.foobar.demo.configuration.FooBarConfiguration;
@RestController
@RequestMapping("/beans")
public class FooBarController {
@Autowired
private FooBarConfiguration fooBarConfiguration;
@GetMapping("/foo")
public Foo getFoo() {
return fooBarConfiguration.getFoo();
}
@GetMapping("/bar")
public Bar getBar() {
return fooBarConfiguration.getBar();
}
@GetMapping("/all")
public List<Object> getAll() {
List list = new ArrayList();
list.add(getFoo());
list.add(getBar());
return list;
}
}application.properties fileScreenShot |
0 replies
VisweshwaranMSpringBootFoobarAppApplication.java@SpringBootApplication
public class SpringBootFoobarAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFoobarAppApplication.class, args);
}
@Bean
public Foo getFoo() {
return new Foo("Visweshwaran");
}
@Bean
public Bar getBar() {
Bar bar = new Bar("Muralidaran", 26);
bar.setFoo(getFoo());
return bar;
}
}Bar.java@Component
public class Bar {
@Value("VisweshwaranM - DEFAULT BAR")
private String name;
@Value("10")
private int age;
private Foo foo;
public Bar() {
}
public Bar(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return new StringBuilder().append("Bar [name=").append(name).append(", age=").append(age).append(", foo=")
.append(foo).append("]").toString();
}
public String printDataVar() {
return new StringBuilder().append("\n First Name: ").append(this.name).append("\n Last Name: ")
.append(this.foo.getName()).append("\n Age: ").append(this.age).append("\n").toString();
}
}
Foo.java@Component
public class Foo {
@Value("VisweshwaranM - DEFAULT FOO")
private String name;
public Foo() {
}
public Foo(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return new StringBuilder().append("Foo [name=").append(name).append("]").toString();
}
}
FoobarController.java@RestController
@RequestMapping("/api/beans")
public class FoobarController {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
@GetMapping("/foo")
public String fooBeanDetails() {
return foo.toString();
}
@GetMapping("/bar")
public String barBeanDetails() {
// return bar.printDataVar();
return new StringBuilder().append("\n First Name: ").append(bar.getName()).append("\n Last Name: ").append("")
.append("\n Age: ").append(bar.getAge()).append("\n").toString();
}
@GetMapping("/all")
public List<String> allBeanDetails() {
return List.of(foo.getName(), bar.getName());
}
}
HomeController.java@RestController
public class HomeController {
@GetMapping("/api")
public String homePage() {
return "Hello visitor <br> Visiting time:"
+ DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm a").format(LocalDateTime.now());
}
}
Screenshots |
0 replies
ComponentsFoo.java package com.springboot.demo.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Foo {
@Value("Manish Malhotra")
private String name;
public Foo() {
super();
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Bar.java package com.springboot.demo.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Bar {
@Value("Manish Malhotra")
private String name;
@Value("32")
private int age;
@Autowired
private Foo foo;
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Bar() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}
ControllersHomeController.java package com.springboot.demo.controllers;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HomeController {
private String message = "Hello Visitor! \n Visiting Time: "+new Date();
@GetMapping("")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}FoobarController.java package com.springboot.demo.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.demo.beans.Bar;
import com.springboot.demo.beans.Foo;
@RestController
@RequestMapping("/api/beans")
public class FoobarController {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
@GetMapping("/foo")
public String getFoo() {
return foo.toString();
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@GetMapping("/bar")
public String getBar() {
return bar.toString();
}
public void setBar(Bar bar) {
this.bar = bar;
}
@GetMapping("/all")
public String getAllBeans() {
return foo.toString() + "\n" + bar.toString();
}
}Application Driverpackage com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.springboot.demo.controllers.FoobarController;
@SpringBootApplication
public class SpringbootFoobarAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFoobarAppApplication.class, args);
//FoobarController controller = new FoobarController();
System.out.println("Spring Boot Foo Bar Application Works!!");
}
}Application PropertiesNone ConfigurationFoobarConfiguration.java package com.springboot.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.springboot.demo.beans.Bar;
import com.springboot.demo.beans.Foo;
@Configuration
public class FoobarConfiguration {
@Bean
public Foo getFoo() {
return new Foo("Manish Malhotra");
}
@Bean
public Bar getBar() {
Bar bar = new Bar("Manish Malhotra",32);
bar.setFoo(getFoo());
return bar;
}
}
|
0 replies
SpringBootFoobarAppApplication Classpackage com.spring.boot.foobar.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootFoobarAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFoobarAppApplication.class, args);
}
}ComponentsBar Classpackage com.spring.boot.foobar.demo.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Bar {
@Value("Rahul")
private String name;
@Value("27")
private int age;
@Autowired
private Foo foo;
public Bar() {
super();
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}Foo Classpackage com.spring.boot.foobar.demo.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Foo {
@Value("ABCD")
private String name;
public Foo() {
super();
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}ControllersHome Controllerpackage com.spring.boot.foobar.demo.controller;
import java.sql.Timestamp;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/api")
public String welcomeMessage()
{
return "Hello visitor!\nVisiting time: " + new Date();
}
}FooBar Controllerpackage com.spring.boot.foobar.demo.controller;
import java.sql.Timestamp;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/api")
public String welcomeMessage()
{
return "Hello visitor!\nVisiting time: " + new Date();
}
}FooBar Configurationpackage com.spring.boot.foobar.demo.controller;
import java.sql.Timestamp;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/api")
public String welcomeMessage()
{
return "Hello visitor!\nVisiting time: " + new Date();
}
}ScreenshotsWelcome MessageFoo detailsBar detailsDetails of all instances |
0 replies
BeansFoopackage com.spring.boot.foobar.demo.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Foo {
@Value("Jits")
private String name;
public Foo() {
super();
// TODO Auto-generated constructor stub
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Barpackage com.spring.boot.foobar.demo.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Bar {
@Value("Makvana")
private String name;
@Value("${bar.age:27}")
private int age;
@Autowired
private Foo foo;
public Bar() {
super();
// TODO Auto-generated constructor stub
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}HomeControllerpackage com.spring.boot.foobar.demo.controllers;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/home/controller")
public class HomeController {
@GetMapping("/api")
public String homePage() {
return "Hello visitor <br> Visiting time:"
+ DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm a").format(LocalDateTime.now());
}
}FooBarControllerpackage com.spring.boot.foobar.demo.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spring.boot.foobar.demo.beans.Bar;
import com.spring.boot.foobar.demo.beans.Foo;
@RestController
@RequestMapping("/api/beans")
public class FoobarController {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
@GetMapping("/foo")
public Foo getFoo() {
return foo;
}
@GetMapping("/bar")
public String getBar() {
return bar.toString();
}
}DriverClasspackage com.spring.boot.foobar.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootFoobarAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFoobarAppApplication.class, args);
}
}
|
0 replies
BeansFoo.javapackage com.example.demo.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Foo {
@Value("Atharv")
private String name;
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Bar.javapackage com.example.demo.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Bar {
@Value("Atharv Chavan")
private String name;
@Value("30")
private int age;
@Autowired
private Foo foo;
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}DriverDemoApplication.classpackage com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}ControllersHomeController.javapackage com.example.demo.controller;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/api")
public String welcomeVisitor() {
return "Hello visitor!Visiting time: "
+ new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp(System.currentTimeMillis()));
}
}FooBarController.javapackage com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.beans.Bar;
import com.example.demo.beans.Foo;
@RestController
@RequestMapping("/api/beans")
public class FooBarController {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
@GetMapping("/foo")
public String fooBeanDetails() {
return foo.toString();
}
@GetMapping("/bar")
public String barBeanDetails() {
return bar.toString();
}
@GetMapping("/all")
public String allBeanDetails() {
return foo.toString()+" "+bar.toString();
}
}Screenshots |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
















Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
FooBar application with Spring Boot
Using
@RestController,@Componentand@BeanCreate a Spring Boot application named
spring-boot-foobar-appwith the following setup configurations:The details for creating the Spring Boot project are as follows:
spring-boot-foobar-appcom.spring.bootspring-boot-foobar-appcom.spring.boot.foobar.demoDependencies to include:
Spring Web
Spring Boot DevTools (optional)
Spring Configuration Processor (optional)
Create these sub-packages under the base package:
com.spring.boot.foobar.demo.beanscom.spring.boot.foobar.demo.controllersCreate two Component classes:
FooandBar, under thecom.spring.boot.foobar.demo.beanspackage with the following details:Foowill have anamedata member of typeString.Barclass will have three data members:String name,int age, and a dependency (object reference)Foo foo.toString()method.(Hint - You can refer to discussion Task - Spring - FooBar App #30 for code snippets to reuse.)
Write
@Beandefinitions for the components in the Configuration class.Create the following Controller classes in
com.spring.boot.foobar.demo.controllerspackage:HomeController: Shows a welcome message to the visitor upon visiting the API base URLFoobarController: Autowire theFooandBarbeans and print both the bean objects using two different handler methods with GET mappings.Create the handler methods and the following mappings in the appropriate controllers:
/api- GET - Displays a welcome message "Hello visitor!\nVisiting time: ${CURRENT_TIMESTAMP}"/api/beans/foo- GET - Displays the details of an instance ofFoobean class./api/beans/bar- GET - Displays the details of an instance ofBarbean class./api/beans/all- GET - Displays a List of instances of every bean in the application.Demonstrate the working with code snippets and screenshots. (You can use this template for submitting solutions)
All reactions