Spring Boot services · Chapter 12 of 41

Validating input

By the end of this chapter you can declare the rules for a request on the request itself, and have Spring refuse bad input with 400 before your method runs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: REST APIs
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The Tapri app went live at five stations, and within a day the orders were full of nonsense: no name, the phone number 12345, a station called BLR (Bengaluru is SBC), zero cutting chai, forty lines of Parle-G. Anjali, who answers the customers, asks for one thing: "Tell people what is wrong before we take their money."

Today OrderController stores whatever arrives:

$ curl -i -X POST localhost:8081/orders -H "Content-Type: application/json" -d '{"customerName": "", "email": "x", "phone": "12345", "station": "BLR", "lines": [{"item": "Cutting chai", "quantity": 0}]}'
HTTP/1.1 201 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 07 Oct 2026 10:08:20 GMT

{"id":1,"customerName":"","station":"BLR","items":0}

You could write an if for every rule in every handler, but that is dozens of checks, and the next endpoint that takes an order forgets half of them.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    OrderRequest.java: refuse a blank name or a bad email

    Make POST /orders check its OrderRequest body and answer 400 Bad Request, without running place, when customerName is missing (null), empty or only spaces, or when email is empty or not an email address ("anjali-at-tapri").

    A valid order (Anjali, anjali@tapri.in, 9876543210, SBC, two Masala chai) still answers 201 Created. The rules go on the record's components in OrderRequest.java, and place's body parameter must ask for them to be checked.

  2. Task 2

    OrderRequest.java: check the order lines too

    Make POST /orders answer 400 Bad Request when lines is missing (null) or empty, when it has more than 10 lines, or when any line has a blank item or a quantity below 1 or above 20 (0 and 21 are refused).

    The limits are allowed: ten lines of quantity 20 each still answer 201 Created. The line rules go on OrderLine, and the list must make the validator check every line.

  3. Task 3

    OrderRequest.java: accept only an Indian mobile number

    Make POST /orders answer 400 Bad Request unless phone is exactly ten digits and the first one is 6, 7, 8 or 9. Refused: 12345, 5876543210 (starts with 5), 98765432101 (eleven digits) and "98765 4321" (a space inside).

    Accepted with 201 Created: 9876543210 and 6123456789.

  4. Task 4

    StationCode.java: write the @StationCode check and use it

    Write StationCode.CodeValidator.isValid(value, context) so it accepts exactly the codes in STATIONS (SBC, NDLS, CSMT, MAS and HWH), case-sensitive: "sbc" and "XYZ" are refused. Return true for null: whether a station is required is not this constraint's business.

    Then put @StationCode on OrderRequest's station. POST /orders answers 400 Bad Request for station XYZ or sbc and 201 Created for NDLS. Validating an order whose only fault is station XYZ gives exactly one violation, on the property station, with the message "is not a Tapri station" (keep the annotation's message as it is).

  5. Task 5

    OrderController.java: validate a query parameter and a path variable

    Make GET /orders answer 400 Bad Request when limit is below 1 or above 50 (0 and 51 are refused), and 200 for a limit from 1 to 50 or no limit at all (the default stays 10).

    Make GET /stations/{code}/orders answer 400 Bad Request for a code that is not a Tapri station (XYZ) and 200 with that station's orders for one that is (HWH), using @StationCode from task 4. Do not put @Validated on the controller: it turns these 400s into 500s.

What you will learn

  • Constraint annotations (@NotBlank, @Email, @Size, @Min, @Max, @Pattern): rules written on a record's components.
  • @Valid: asks Spring to check a request body, and the validator to walk into nested objects.
  • A custom constraint: an annotation plus a validator class, for a rule no built-in constraint has.
  • Parameter validation: the same rules on query parameters and path variables.

More in REST APIs

Build "Validating input" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.