The missing field, and the body that is not JSON
The POST without a departmentId, the one the lecture 6 backend answered with a 500:
$ curl -s -i -X POST localhost:8087/api/employees -H 'Content-Type: application/json' -d '{"name":"Mari Maasikas","email":"mari@example.com"}'
HTTP/1.1 400
{"status":400,"message":"validation failed","path":"/api/employees","timestamp":"…","errors":{"departmentId":"must not be null"}}
A body that cannot be parsed never reaches validation; Spring throws HttpMessageNotReadableException while reading it. One more handler method, same shape:
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ApiError unreadable(HttpMessageNotReadableException e, HttpServletRequest request) {
return ApiError.of(400, "request body is not valid JSON", request.getRequestURI());
}
$ curl -s -i -X POST localhost:8087/api/employees -H 'Content-Type: application/json' -d '{"name": "Mari'
HTTP/1.1 400
{"status":400,"message":"request body is not valid JSON","path":"/api/employees","timestamp":"…"}