INTRODUCTION

We are learning how to create simple spring boot project. How spring boot helps us to create project in simple way. please refer below  spring boot project with workflow.

Creating a spring boot project :


1. Open spring initializr (https://start.spring.io.)

2. Please select Maven or Gradle project as per requirement

3. Please select programming language for project.

4. Please select spring boot version as per requirement.

5. Please provide the group and artifact name, we have provided group com.codingknowledge and artifact is spring-example.




6. Click on GENERATE button.


7. RAR file is downloaded , please extract the RAR file.


8. Import the extracted folder in your suitable IntelliJ or eclipse.

FILE --> IMPORT --> EXISTING MAVEN PROJECT --> NEXT --> SELECT PROJECT--> FINISH.


9. After importing , Project directory looks like below.



10. Main method is created automatically.

code snippet : 

SpringExampleApplication.class.


@SpringBootApplication
public class SpringExampleApplication {

   public static void main(String[] args) {

      SpringApplication.run(SpringExampleApplication.class, args);
   }

}

after running above project selecting 1 java application,

Create REST API : 


1. Follow above steps to create spring boot project.


2. Create a model class, we have created one model class StudentDetails.class.




@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class StudenDetails{
    private String rollNo;

    private String studentName;

    private String marks;

}

We have annotated class with @Data, @AllArgsConstructor, @NoArgsConstructor and @Builder please refer my blog for more details click here.


3. In your package , create controller package and inside create class, example in com.vat package create package controller and inside controller package create class name  StudentController.class.

This class is annotated with @RestController annotation.



Code Snippet : 

@RestController
public class StudentController {

    @Autowired
    StudenService studenService;

    @GetMapping("/getStudentData")
    public ResponseEntity<StudentDetails> validateAuthRequest() throws Exception {

    return new ResponseEntity<StudentDetails>(studenService.getStudentData(),HttpStatus.OK);
    }
}


Explanation

@Getmapping is for which http method you are referring, like for post it is @Postmapping , for put it is @Putmapping.

inside @GetMapping need to add url.


4. Create Interface package in com.vat package , example in com.vat create service package and inside package create interface StudentService.class





public interface StudenService {
    StudentDetails getStudentData() throws Exception;
}

NOTE : This interface class is @Autowired in controller class to call Implementation layer.



5. Create serviceImpl package and inside create the Impl class. This serviceImpl class is act as Implementation layer so all logic and code implementation is added in this class file. 

example, create serviceImpl package inside com.vat package and create Impl class file as StudentServiceImpl.class and it implements the Interface so we need to provide the implementation or override the interface method. 

this class is annotated with @Service annotation represent the implementation layer.



@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public StudentDetails getStudentData() throws Exception {

        StudentDetails studentDetails = new StudentDetails();
        studentDetails.setName("Suraj");
        studentDetails.setRollNo("1");
        studentDetails.setMarks("98");

        return studentDetails;
    }
}

So when we run the application as spring boot application and starting at 8080 port number.


Below is postman screenshot, postman is a tool used by developer to test their Rest API.


In postman we need to select HTTP Method , need to add URL and Need to click on send button.

Http method : GET

URLlocalhost:8080/student/getStudentData.

Response

{
    "rollNo""1",
    "name""Suraj",
    "marks""98"
}


In above application we are returning hardcoded data, now we are

calling DOA layer that repository class to get the data from database.


6. create repository package under com.vat package and inside repository create StudentRepository.class.

The class will extends JPARepository<> and @Respository annotation as class level.


public class StudentRepository extends JPARepository<StudentDetails, String>{

    @Query(value = "select * from apx.student(:rollNo);", nativeQuery = true)
    Student getTransactionData(@Param("roll_np") String rollNo);
}

And Implementation Layer code changes, as we are calling repository layer from service class and fetching data from database and return it.

@Service
public class StudentServiceImpl implements StudenService {

    @Autowired
    StudentRepository studentRepository;
    @Override
    public StudentDetails getStudentData() throws Exception {

        StudentDetails student = studentRepository.getTransactionData();

        return student;
    }
}


IMPORTANT POINTS : 

1. As spring boot project server up on default port number 8080, we can change port number using application.properties file.

server.port : 5008 

2. We need to give the path for project URL so we can add it in application.properties file.

server.servlet.context-path=/student

3. Sometimes we need to add URL, username, password, some configurations so we can add it in application.properties file.


SUMMARY

So we have learned how we can create spring boot project, Rest API in simple way.


ABOUT US

I am Suraj Ladda and I am a Software Developer in IT company.

Contact US

surajladda07@gmail.com