Skip to content

alanngo/SpringBoot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot

Prerequisites

Getting Started

  1. 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
  2. Click "Generate"

  3. Unzip the download to your working directory

  4. 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>

File Structure

com/example/api
com/example/dto
com/example/entity
com/example/repository
com/example/service
com/example/utility

Integrating Lombok

IntelliJ:

https://www.youtube.com/watch?v=RwX1x7_k1b0

  1. Depending on your OS
    • Windows/ Linux: File -> Settings -> Plugins (Ctrl + Alt + S)
    • Mac: IntelliJ Idea -> Preferences -> Plugins
  2. Click on 'Marketplace tab' and search 'lombok'
  3. Click Install
  4. Click Apply and restart IntelliJ
Errors and Solutions :

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.

  1. Open Settings/Preferences -> Compiler
  2. Add -Djps.track.ap.dependencies=false to 'Shared build process VM options'
  3. Apply and OK
  4. Rerun your program

https://i.stack.imgur.com/jwTAw.png

Important Annotations

API
// 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
DTO
// Class Based
@Data // constructor, getters, setters, equals, hashCode, toString 
Entity
// 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
Repository
  • none
Service
// Class Based
@Service // denotes service layer
@Transactional // allows the class to update DB fields

// Field Based
@Autowired // dependency injection
Utility

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

Repository Layer

public interface ExampleRepository extends CrudRepository<ENTITY, ID> 
{
    // SQL based programs use CrudRepository
}

About

Really Relevant in alot of industries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages