8: spring-boot-first-app

 

1-- Main Method of spring boot


spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  SpringBootFirstApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootFirstAppApplication {

    public static void main(String[] args) {
    SpringApplication.run(SpringBootFirstAppApplication.class, args);
    }

}




2-  A string that returns "Hello World" 

     When client writes" http://localhost:8080/hello-world " on browser


spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  HelloWorldController.java

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorldController {
   
    //GET HTTP Method
    //When client writes" http://localhost:8080/hello-world " then helloWorld() method executed
   
    @GetMapping("/hello-world")
    public String helloWorld() {
        return"Hello World!";
    }

Image 1 



3- Student class for student information(Getter, Setter and Constructors)

spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  Student.java

package com.example.demo;


public class Student {
   
    private String firstName;
    private String lastName;
   
    public Student(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
   
   
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

 }


4-   One Student information i.e(firstName and lastName) return in json

       When client writes "http://localhost:8080/student"


spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  StudentController.java

package com.example.demo;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    //    http://localhost:8080/student
    //    getStudent() method called
   
   
       @GetMapping("/student")
       public  Student getStudent() {
        return new Student("Zeeshan","Khan");
       }
}

Image 2



5-   More than One Student information i.e(firstName and lastName) return

   When client writes "http://localhost:8080/students" on browser


spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  StudentController.java


package com.example.demo;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    //    http://localhost:8080/student
    //    getStudent() method called
   
   
       @GetMapping("/student")
       public  Student getStudent() {
        return new Student("Zeeshan","Khan");
       }
       
        @GetMapping("/students")
        public ArrayList<Student> getStudents(){
            ArrayList<Student> students= new ArrayList<>();
            students.add(new Student("Tony","Stark"));
            students.add(new Student("Morgan","Stark"));
            students.add(new Student("Thanos","Stark"));
       
            return students;
        }
}

Image 3


6-    Return the student information we asked for 

   When client writes "http://localhost:8080/student/{firstName}/{lastName}"


spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  StudentController.java

package com.example.demo;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    //    http://localhost:8080/student
    //    getStudent() method called
   
   
       @GetMapping("/student")
       public  Student getStudent() {
        return new Student("Zeeshan","Khan");
       }
       
        @GetMapping("/students")
        public ArrayList<Student> getStudents(){
            ArrayList<Student> students= new ArrayList<>();
            students.add(new Student("Tony","Stark"));
            students.add(new Student("Morgan","Stark"));
            students.add(new Student("Thanos","Stark"));
       
            return students;
        }



// http://localhost:8080/student/tony/stark
//Write a rest api which will handle this kind of request
/*    uri template variable [@GetMapping("{firstName}/{lastName}")] is connected by methods
      firstName and lastName by @PathVariable */
       
        @GetMapping("/student/{firstName}/{lastName}")
        public Student studentPathVariable(@PathVariable("firstName")String firstName,
@PathVariable("lastName")String lastName) {
            return new Student(firstName, lastName);
        }
   }    

Image 4

Image 5


7-  A REST API with concept of Queries in it

spring-boot-first-app   -->    src/main/java  -->   com.example.demo  -->  StudentController.java

package com.example.demo;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    //    http://localhost:8080/student
    //    getStudent() method called
   
   
       @GetMapping("/student")
       public  Student getStudent() {
        return new Student("Zeeshan","Khan");
       }
       
        @GetMapping("/students")
        public ArrayList<Student> getStudents(){
            ArrayList<Student> students= new ArrayList<>();
            students.add(new Student("Tony","Stark"));
            students.add(new Student("Morgan","Stark"));
            students.add(new Student("Thanos","Stark"));
       
            return students;
        }



// http://localhost:8080/student/tony/stark
//Write a rest api which will handle this kind of request
/*    uri template variable [@GetMapping("{firstName}/{lastName}")] is connected by methods
      firstName and lastName by @PathVariable */
       
        @GetMapping("/student/{firstName}/{lastName}")
        public Student studentPathVariable(@PathVariable("firstName")String firstName,@PathVariable("lastName")String lastName) {
            return new Student(firstName, lastName);
        }
       
        // http://localhost:8080/student/query?firstName=Tony&lastName=Stark
      //Write a rest api which will handle this kind of request
      /*   firstName from GetMapping and from new Student are binded by @RequestParam */
             
       
       
        @GetMapping("/student/query")
        public Student studentQueryParam(
                @RequestParam(name="firstName")String firstName,
                @RequestParam(name="lastName")String lastName
                ){
            return new Student(firstName,lastName);
        }
       
}

Image 6



pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.app</groupId>
    <artifactId>spring-boot-first-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-first-app</name>
    <description>First Application created using spring boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
       
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



       
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot