StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 3 · Validation and Error Handling

Pizza Order Constraint Validator

easy100%1 of 1 solved

Problem statement

Spring Boot: Pizza Order Constraint Validator

Build a REST API for validating pizza orders that reports every broken rule in one uniform error response.

Requirements

OrderRuleValidator

The provided controller hands every parsed order to the validate method and rejects the order whenever the returned list is non-empty, so validate must return the list of broken rule messages and an empty list (never null) for a valid order. When the list comes back empty the controller answers 200 OK with a body carrying the order's size and toppings exactly as submitted, with the toppings in their original order. Check the following rules and collect the exact message of every rule the order breaks:

  • If size is missing, add "Size is required"
  • If size is present but not exactly one of small, medium or large, add "Size must be small, medium or large"; the value is compared exactly, so a different casing such as Large is not accepted
  • If toppings is missing, add "Toppings are required"; an empty toppings list is a valid order without toppings
  • If the order has more than 6 toppings, add "A pizza allows at most 6 toppings"; exactly 6 toppings are valid
  • If any topping appears more than once, add "Toppings must not contain duplicates"; repeated toppings do not need to be adjacent, and the message is added once no matter how many toppings repeat
  • Toppings are compared exactly, so Ham and ham count as different toppings and do not break the duplicate rule
  • If size is small and the order has more than 3 toppings, add "A small pizza allows at most 3 toppings"; a small pizza with exactly 3 toppings is valid, and this rule never applies to a medium or large pizza
  • A missing size adds only "Size is required" and never the allowed-values message as well; likewise a missing toppings adds only "Toppings are required" and none of the topping rules

GlobalExceptionHandler

Wire the two provided handler methods so that every error response leaves the API in the shared shape below.

  • When the order breaks one or more rules, respond 422 Unprocessable Entity with a JSON body holding exactly the fields timestamp, status, code, errors and path (and no message field)
  • In the 422 body, timestamp is the time the response was produced, status is the number 422, code is "RULE_VIOLATION", path is the request path, and errors lists the message of every broken rule in alphabetical order
  • When the request body is missing or cannot be parsed as JSON, the rules are never checked: respond 400 Bad Request with a JSON body holding exactly the fields timestamp, status, code, message and path (and no errors field)
  • In the 400 body, status is the number 400, code is "MALFORMED_JSON" and message is "Malformed JSON request"
  • Broken rules never produce a 400 Bad Request, and an unparseable body never produces a 422 Unprocessable Entity

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /orders

Request:

{
   "size": "medium",
   "toppings": ["ham", "pineapple"]
}

Response (200 OK):

{
    "size": "medium",
    "toppings": [
        "ham",
        "pineapple"
    ]
}

POST /orders

Request:

{
   "size": "small",
   "toppings": ["ham", "olive", "mushroom", "pepper"]
}

Response (422 Unprocessable Entity):

{
    "timestamp": "2026-07-30T10:00:39.152519",
    "status": 422,
    "code": "RULE_VIOLATION",
    "errors": [
        "A small pizza allows at most 3 toppings"
    ],
    "path": "/orders"
}

POST /orders

Request:

{
   "size": "small",
   "toppings": ["ham", "olive", "mushroom", "pepper", "onion", "bacon", "ham"]
}

Response (422 Unprocessable Entity):

{
    "timestamp": "2026-07-30T10:00:39.324182",
    "status": 422,
    "code": "RULE_VIOLATION",
    "errors": [
        "A pizza allows at most 6 toppings",
        "A small pizza allows at most 3 toppings",
        "Toppings must not contain duplicates"
    ],
    "path": "/orders"
}

POST /orders

Request:

{}

Response (422 Unprocessable Entity):

{
    "timestamp": "2026-07-30T10:00:39.483154",
    "status": 422,
    "code": "RULE_VIOLATION",
    "errors": [
        "Size is required",
        "Toppings are required"
    ],
    "path": "/orders"
}

POST /orders

Request:

{ this is not json

Response (400 Bad Request):

{
    "timestamp": "2026-07-30T10:00:39.666679",
    "status": 400,
    "code": "MALFORMED_JSON",
    "message": "Malformed JSON request",
    "path": "/orders"
}

More in Validation and Error Handling

See the module
Passport Application Form ValidatorThis problem covers checking an application form field by field before it is accepted, including an age cutoff on the date of birth.50%1 of 2 solvedeasyBlood Donation Eligibility CheckThis problem covers writing your own validation rule that looks at several fields together and reports the first thing that is wrong.100%1 of 1 solvedeasyGlobal Error Response StandardizerThis problem covers turning every kind of failure in the application into one consistent error response.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.