R
Rishtaara
Knowledge Hub
Technology & IT

Spring Boot Fundamentals: Java Backend Guide

By Rishtaara Editorial Team40 min read
#Spring Boot#Java#REST#JPA

DI, REST controllers, Spring Data JPA, security, and testing with examples.

Why Spring Boot

Spring Boot reduces boilerplate around Java backend development by offering opinionated defaults, embedded servers, and production-ready tooling.

  • Auto-configuration for common stacks.
  • Starter dependencies for rapid setup.
  • Embedded Tomcat/Jetty for easy runs.
  • Actuator endpoints for monitoring.

IoC and DI

Inversion of Control means the container creates and wires objects for you. Constructor injection keeps dependencies explicit and testable.

Service with constructor injection
@Service
public class CourseService {
  private final CourseRepository courseRepository;

  public CourseService(CourseRepository courseRepository) {
    this.courseRepository = courseRepository;
  }

  public List<Course> listCourses() {
    return courseRepository.findAll();
  }
}

Bootstrapping a project

  • Use Spring Initializr with Web, JPA, Validation dependencies.
  • Set metadata: group, artifact, Java version.
  • Externalize config using `application.yml` or `application.properties`.
Basic application.yml
server:
  port: 8080

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/rishtaara
    username: postgres
    password: postgres
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

REST endpoint design

  • Use nouns in routes (`/courses`, `/students`).
  • Map HTTP verbs clearly: GET, POST, PUT/PATCH, DELETE.
  • Return proper status codes and structured JSON.
CourseController example
@RestController
@RequestMapping("/api/courses")
public class CourseController {
  private final CourseService courseService;

  public CourseController(CourseService courseService) {
    this.courseService = courseService;
  }

  @GetMapping
  public List<CourseDto> list() {
    return courseService.list();
  }

  @PostMapping
  public ResponseEntity<CourseDto> create(@Valid @RequestBody CreateCourseRequest request) {
    CourseDto created = courseService.create(request);
    return ResponseEntity.status(HttpStatus.CREATED).body(created);
  }
}

Entity and repository basics

JPA entity and repository
@Entity
public class Course {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String title;
  private String level;
}

public interface CourseRepository extends JpaRepository<Course, Long> {
  List<Course> findByLevel(String level);
}

JPA tips

  • Prefer DTOs at API boundaries to avoid lazy-loading leaks.
  • Use pagination for list endpoints.
  • Model relationships carefully (`@OneToMany`, `@ManyToOne`).

Authentication and authorization

Authentication answers who the user is. Authorization answers what the user can access. Spring Security filters enforce both before your controller runs.

Minimal security config

Route-level restrictions
@Configuration
@EnableWebSecurity
public class SecurityConfig {
  @Bean
  SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
      .csrf(csrf -> csrf.disable())
      .authorizeHttpRequests(auth -> auth
        .requestMatchers("/api/public/**").permitAll()
        .anyRequest().authenticated()
      )
      .httpBasic(Customizer.withDefaults())
      .build();
  }
}

Testing strategy

  • Unit tests for service logic with mocks.
  • Slice tests (`@WebMvcTest`) for controller contracts.
  • Integration tests for repository and full app flow.

Controller test example

MockMvc happy-path test
@WebMvcTest(CourseController.class)
class CourseControllerTest {
  @Autowired private MockMvc mockMvc;
  @MockBean private CourseService courseService;

  @Test
  void shouldReturnCourseList() throws Exception {
    when(courseService.list()).thenReturn(List.of(new CourseDto(1L, "Spring Boot", "Beginner")));

    mockMvc.perform(get("/api/courses"))
      .andExpect(status().isOk())
      .andExpect(jsonPath("$[0].title").value("Spring Boot"));
  }
}

What you will build

Build a production-style Course Catalog REST API with CRUD, filtering, validation, role-based authorization, and test coverage.

  • Endpoints for create/list/update/delete courses.
  • Pagination and search by level/topic.
  • Security for admin-only write operations.
  • Seed script and API docs.

Project checklist

  • Model entities and DTOs.
  • Implement service + repository layers.
  • Add exception handling with consistent error schema.
  • Write unit and integration tests before final review.

Key Takeaways

  • Spring Boot accelerates backend delivery with strong defaults.
  • Dependency injection improves modularity and testability.
  • REST + JPA + validation forms a practical backend core.
  • Security and testing should be built in from day one.
  • A complete API project consolidates architecture decisions.