Sunday 18 February 2018

Spring boot sample project with basic CRUD operations

This project demonstrates the basic spring boot project. This can serve as a good template for any project where you need to perform CRUD operations using REST services.

You can also download the sample project from start.spring.io

If you don't want to use the start.spring.io then you can simply create a simple maven project in eclipse and add the dependencies manually in pom.xml. This also works fine.

The important part is pom.xml where you specify all the dependencies. Including parent and specifying the version of spring-boot-starter-parent in pom.xml allows you not to bother about the version of dependencies that you include as they are automatically chosen by spring-boot framework (without you specifying the version of each dependency).

Don't forget to override the java.version property (see pom.xml below) as the default provided by spring-boot-starter-parent may be the one that you don't want.

  ___________________________________________________________________________

The following template project is for maintaining various subjects and performing CRUD operations.

I assume that each subject has an id, name and description. The id is used to uniquely identify a subject.

Note that I follow the general convention of sending the request type which is as follows:
  1. For getting all the subjects, send a GET request. 
  2. For adding a new subject, send a POST request.
  3. For updating an existing subject, send a PUT request.
  4. For deleting an existing subject, send a DELETE request.

Find the complete source code of this template project here on github.

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>org.springboottest.com</groupId>
 <artifactId>subject-course-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>subject-course-test</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.0.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <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>



Subject.java
package org.springboottest.com.subject;

public class Subject {

 private String id,name,description;

 public Subject() {
  
 }
 
 public Subject(String id, String name, String description) {
  super();
  this.id = id;
  this.name = name;
  this.description = description;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }
 
 
}


SubjectController.java
package org.springboottest.com.subject;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SubjectController {

 @Autowired
 SubjectService subjectService;
 
 @RequestMapping("/subjects")
 public List<Subject> someFunction() {
  return subjectService.getAllSubjects();
 }
 
 @RequestMapping("/subject/{subjectId}")
 public Subject getSubjectById(@PathVariable String subjectId) {
  return subjectService.getSubjectById(subjectId);
 }
 
 @RequestMapping(method=RequestMethod.POST, value="/subjects")
 public void addSubject(@RequestBody Subject subject) {
  subjectService.getAllSubjects().add(subject);
 }
 
 @RequestMapping(method=RequestMethod.PUT, value="/subjects")
 public void updateSubject(@RequestBody Subject subject) {
  subjectService.updateSubject(subject);
 }
 
 @RequestMapping(method=RequestMethod.DELETE, value="/subject/{subjectId}")
 public void deleteSubject(@PathVariable String subjectId) {
  subjectService.deleteSubjectById(subjectId);
 }
 
}


SubjectService.java
package org.springboottest.com.subject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class SubjectService {

 List<Subject> subjects = new ArrayList<>(Arrays.asList(
   new Subject("AI","Artificial Intelligence", "Artificial Intelligence description"),
   new Subject("ML","Machine Learning", "Machine Learning description"),
   new Subject("Algo","Algorithms and Complexity", "Algorithms and Complexity description"),
   new Subject("DS","Data Structures", "Data Structures description")
   ));
 
 public List<Subject> getAllSubjects(){
  return subjects;
 }
 public Subject getSubjectById(String id) {
  return subjects.stream().filter(s->s.getId().equals(id)).findFirst().get();
 }
 public void updateSubject(Subject subject) {
  for(int i=0;i<subjects.size();i++) {
   if(subjects.get(i).getId().equals(subject.getId())) {
    subjects.set(i, subject);
    return;
   }
  }
 }
 public void deleteSubjectById(String subjectId) {
  subjects.removeIf(s->s.getId().equals(subjectId));
 }
 
}


App.java
package org.springboottest.com;

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

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  ApplicationContext context = SpringApplication.run(App.class, args);
 }
}