The service response
The code and the short line the client will see.
A small handler does not print a stack trace to the client. It picks a code and a short reason. 200 OK means the request was understood and done. 404 Not Found means the route or object was not found; that is not a "server error". 500 is kept for an unexpected crash, not for a malformed form field.
The response text and the code are a pair. You cannot return a "success" body with a 404. In the task the pair is already glued into one line: 200 OK or 404 Not Found. In real code this is the status line plus a separate body, but the rule is the same: the code and the meaning do not contradict each other.
Input validation happens before you pick the happy code. An empty name is a 400, not a 200 with an empty user in the database. A format exception also becomes a 400 at the boundary instead of flying through as a 500, once you have recognized it.
The task method is pure: a code goes in, a string comes out. No socket, no file. That is also how to start reading someone else's controller: first the pure decision function, then the HTTP wiring. The wiring is easier to replace than a rule smeared across filters.
Practice
Which outcome is this
The route does not exist. Which response class is more honest?
- 200 OK
- 404 Not Found
- always 500
- shut down the process
The success line
In the task, what line is returned for code 200? Write it in full.
The status line
Write static String status(int code). For 200 return 200 OK, for 404 return 404 Not Found, otherwise the number, a space and Other.