- One of the Java versions
- IntelliJ
- Postman
- Depending on your database, use one of the following
- NOTE: This repository is for SQL-based projects. For MongoDB related projects, go to https://github.com/alanngo/SpringBoot-Mongo
-
Go to https://start.spring.io/
- Apply the following settings
- Project: Maven
- Language: Java
- Group: com.{project name}
- Artifact: {NAME}
- Name: {NAME}
- Description: Spring Project (Doesn't matter)
- Package name: com.{project name}
- Packaging: Jar
- Java: {Your version of Java installed}
- Add the following Dependencies:
- Lombok
- Spring Web
- Spring Data JPA
- Apply the following settings
-
Click "Generate"
-
Unzip the download to your working directory
-
Manually add the following dependencies in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
com/example/api
com/example/dto
com/example/entity
com/example/repository
com/example/service
com/example/utility
https://www.youtube.com/watch?v=RwX1x7_k1b0
- Depending on your OS
- Windows/ Linux: File -> Settings -> Plugins (Ctrl + Alt + S)
- Mac: IntelliJ Idea -> Preferences -> Plugins
- Click on 'Marketplace tab' and search 'lombok'
- Click Install
- Click Apply and restart IntelliJ
If you are getting
You aren't using a compiler supported by lombok, so lombok will not work and has been disabled
error, follow the steps below.
- Open Settings/Preferences -> Compiler
- Add
-Djps.track.ap.dependencies=false
to 'Shared build process VM options' - Apply and OK
- Rerun your program
https://i.stack.imgur.com/jwTAw.png
// Class Based
@RestController // allows the class to have API routes
@CrossOrigin // allows other programs to consume SpringBoot app
@RequestMapping // root url mapping
// Field Based
@Autowired // dependency injection
// Method Based
@GetMapping("/URL") // allows a method to be called when GET request is made w/ '/URL'
@PostMapping("/URL") // allows a method to be called when POST request is made w/ '/URL'
@PutMapping("/URL") // allows a method to be called when PUT request is made w/ '/URL'
@DeleteMapping("/URL") // allows a method to be called when DELETE request is made w/ '/URL'
// Method Parameter Based
@RequestBody // allows POJO to be parsed as JSON request body
@PathVariable // used for url patterns of *./{pathVar}, method arg name must also be the same
@QueryParam // used for url patterns of ?key=value
// Class Based
@Data // constructor, getters, setters, equals, hashCode, toString
// Class Based
@Data // constructor, getters, setters, equals, hashCode, toString
@Table("TABLE_NAME") // binds class to SQL table if class/table name is different
@Entity // represents class as SQL table
// Field Based
@Id // primary key
@GeneratedValue(strategy = GenerationType.IDENTITY) // auto increment PK
@Column("COL_NAME") // binds field to DB column if field/column name is different
- none
// Class Based
@Service // denotes service layer
@Transactional // allows the class to update DB fields
// Field Based
@Autowired // dependency injection
ErrorInfo
// Class Based
@Data // constructor, getters, setters, equals, hashCode, toString
ExceptionControllerAdvice
// Class Based
@RestControllerAdvice // allows the app to output errors to user in a useful manner
@Slf4j // creates a logger variable automatically
// Field Based
@Autowired // dependency injection
// Method Based
@ExceptionHandler(value = Exception.class) // allows method to be called when exception is raised
LoggingAspect
// Class Based
@Component // denotes spring bean
@Aspect // used for crosscutting concern
@Slf4j // creates a logger variable automatically
// Method Based
@AfterThrowing(pointcut = "execution(CLASS_NAME)") // will execute after throwing exception
public interface ExampleRepository extends CrudRepository<ENTITY, ID>
{
// SQL based programs use CrudRepository
}