StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 4 · JPA Persistence Basics

Beekeeping Hive Log

easy100%1 of 1 solved

Problem statement

Spring Boot: Beekeeping Hive Log

Build a REST API for logging beekeeping hive inspections, backed by a database and layered behind a service.

Requirements

InspectionService

The service owns every read and write of the log; implement its two methods:

  • getInspections returns the stored inspections of the given hive
  • addInspection stores a new inspection for the given hive and returns the stored record, including its generated id

InspectionController

The controller must reach the data only through InspectionService: it must not declare the repository as a field or as a constructor parameter. The starter controller is wired straight to the repository, so rework that wiring to put the service in between. Each endpoint must be reachable at exactly the verb and path listed below.

GET /hives/{hiveId}/inspections

  • Return 200 OK with the inspections of that hive only, taken from the path; inspections of other hives never appear
  • Each returned inspection carries its id, hiveId, inspector, and healthScore
  • Return 200 OK with an empty array when the hive has no inspections (including a hive id that has never been inspected); this endpoint never returns an error status

POST /hives/{hiveId}/inspections

  • Accept a JSON body with inspector and healthScore and validate it
  • If inspector is missing, empty, or whitespace-only, return 400 Bad Request with error message: "Inspector must not be blank"
  • If healthScore is missing, return 400 Bad Request with error message: "Health score is required"
  • If healthScore is less than 1 or greater than 10, return 400 Bad Request with error message: "Health score must be between 1 and 10"; scores of exactly 1 and exactly 10 are valid
  • A rejected request must not store anything
  • On a valid body, store the inspection for that hive and return 201 Created with the stored inspection, including its generated id and the hiveId taken from the path
  • A stored inspection appears in its own hive's list and in no other

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /hives/4/inspections

Request:

{
   "inspector": "June Alvarez",
   "healthScore": 8
}

Response (201 Created):

{
   "id": 1,
   "hiveId": 4,
   "inspector": "June Alvarez",
   "healthScore": 8
}

POST /hives/9/inspections

Request:

{
   "inspector": "Priya Nair",
   "healthScore": 10
}

Response (201 Created):

{
   "id": 3,
   "hiveId": 9,
   "inspector": "Priya Nair",
   "healthScore": 10
}

GET /hives/4/inspections

Response (200 OK):

[
    {
      "id": 1,
      "hiveId": 4,
      "inspector": "June Alvarez",
      "healthScore": 8
    },
    {
      "id": 2,
      "hiveId": 4,
      "inspector": "Sam Okafor",
      "healthScore": 6
    }
]

GET /hives/15/inspections

Response (200 OK):

[]

POST /hives/4/inspections

Request:

{
   "inspector": "",
   "healthScore": 8
}

Response (400 Bad Request):

{
   "error": "Inspector must not be blank",
   "timestamp": "2026-07-30T10:13:36.198631",
   "path": "/hives/4/inspections"
}

POST /hives/4/inspections

Request:

{
   "inspector": "June Alvarez"
}

Response (400 Bad Request):

{
   "error": "Health score is required",
   "timestamp": "2026-07-30T10:13:36.674343",
   "path": "/hives/4/inspections"
}

POST /hives/4/inspections

Request:

{
   "inspector": "June Alvarez",
   "healthScore": 11
}

Response (400 Bad Request):

{
   "error": "Health score must be between 1 and 10",
   "timestamp": "2026-07-30T10:13:37.097807",
   "path": "/hives/4/inspections"
}

More in JPA Persistence Basics

See the module
Plant Watering Schedule StoreThis problem covers the full set of database-backed endpoints for plant schedules, including updating a row in place rather than adding a new one.100%1 of 1 solvedeasyGuitar Lesson Booking StoreThis problem covers letting the database enforce uniqueness and turning the resulting failure into the right response.100%1 of 1 solvedeasyBoard Game Collection TrackerThis problem covers linking games to their owner and reading a sorted list back for one owner at a time.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.