Java Academy Logo

Java Academy

Spring Boot REST API Development

Spring Boot is the most popular framework for building Java REST APIs. It provides auto-configuration, embedded servers, and seamless integration with databases, security, and cloud platforms.

Spring Boot REST Stack

  • spring-boot-starter-web — REST controllers, Jackson JSON, embedded Tomcat.
  • spring-boot-starter-data-jpa — Database access with Hibernate.
  • spring-boot-starter-validation — Bean validation (@NotNull, @Email).
  • spring-boot-starter-security — Authentication and authorization.
  • spring-boot-starter-actuator — Health checks and metrics.

Complete REST Controller

@RestController
@RequestMapping("/api/v1/customers")
@RequiredArgsConstructor
public class CustomerController {
    private final CustomerRepository repo;

    @GetMapping("/{id}")
    public CustomerDto get(@PathVariable Long id) {
        return repo.findById(id)
            .map(CustomerDto::from)
            .orElseThrow(() -> new ResourceNotFoundException(id));
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
        return ResponseEntity.status(404).body(new ErrorResponse(ex.getMessage()));
    }
}

application.yml Configuration

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/myapp
    username: ${DB_USER}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: validate
server:
  port: 8080

Frequently Asked Questions

Spring Boot vs plain Spring?

Spring Boot adds auto-configuration, embedded server, and starter dependencies. You can build REST APIs much faster with minimal boilerplate.

Continue Learning