StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 2 · REST Controllers & Endpoints

Bird Sightings API

easyRequest HandlingQuery Parameters100%2 of 2 solved

Problem statement

Add an optional species filter to the endpoint that lists bird sightings.

Requirements

The Sighting entity, the SightingService and the POST /sightings endpoint are already provided. Your task is to write the GET /sightings endpoint in SightingController.

SightingService

  • findAll() returns every sighting
  • findBySpecies(species) returns the sightings whose species equals the given name exactly

SightingController

GET /sightings

  • Read an optional species query parameter
  • When species is not sent, return 200 OK with every sighting
  • When species is sent, return 200 OK with only the sightings of that species
  • When no sighting has that species, return 200 OK with an empty array, not an error
Sample Requests & Responses

POST /sightings

Request:

{
  "species": "Robin",
  "location": "Willow Creek"
}

Response (201 Created):

{
  "species": "Robin",
  "location": "Willow Creek",
  "id": 1
}

GET /sightings

Response (200 OK):

[
  {
    "species": "Robin",
    "location": "Willow Creek",
    "id": 1
  },
  {
    "species": "Barn Owl",
    "location": "Old Mill",
    "id": 2
  },
  {
    "species": "Robin",
    "location": "Cedar Park",
    "id": 3
  }
]

GET /sightings?species=Robin

Response (200 OK):

[
  {
    "species": "Robin",
    "location": "Willow Creek",
    "id": 1
  },
  {
    "species": "Robin",
    "location": "Cedar Park",
    "id": 3
  }
]

GET /sightings?species=Heron

Response (200 OK):

[]

More in REST Controllers & Endpoints

See the module
Planetarium Show CatalogThis problem covers adding, listing, and removing shows, and what to answer when someone asks for one that isn't there.50%2 of 4 solvedeasyVending Machine InventoryThis problem covers changing stored state from a request, and telling apart a slot that is empty from one that does not exist.100%2 of 2 solvedeasyComic Book Issue APIThis problem covers keeping child records inside their own parent, so one series never shows another series' issues.67%2 of 3 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.