Mastering Spring Boot: Unified Controllers, Validation, and Response Handling
This guide walks through building Spring Boot controller endpoints, standardizing request parameters, applying unified status codes with ResultVo, leveraging @Validated for bean validation, and using @RestControllerAdvice to globally handle exceptions and automatically wrap responses.
Spring Boot controller methods receive requests via @GetMapping and @PostMapping under a common @RequestMapping prefix. The example controller shows a ProductInfoController with a GET method findById and a POST method findPage, illustrating how request parameters are bound to a VO object.
The annotations @RestController (equivalent to @Controller + @ResponseBody), @RequestMapping, @GetMapping, and @PostMapping are explained, as well as how JSON fields such as productId and productName are automatically mapped from the request body.
To improve front‑end collaboration, the article proposes a unified response format that adds a status code, a message msg, and a data payload. Raw entity JSON is contrasted with the wrapped structure, and a StatusCode interface plus a ResultCode enum (e.g., SUCCESS(1000, "请求成功")) are introduced to centralize status definitions.
The ResultVo class provides constructors for different scenarios: returning only a code/message, returning data with the default success code, or returning data with a custom StatusCode. This eliminates repetitive manual wrapping in controller methods.
Parameter validation is streamlined with @Validated and Bean Validation annotations such as @NotNull and @Min. When validation fails, Spring throws BindException, which is intercepted by a @RestControllerAdvice that extracts the first ObjectError and returns a ResultVo using ResultCode.VALIDATE_ERROR.
To avoid writing new ResultVo(...) in every method, a ResponseBodyAdvice implementation ( ControllerResponseAdvice) automatically wraps non‑ ResultVo responses. It treats String return types specially by converting the wrapped object to JSON. Methods annotated with a custom @NotControllerResponseAdvice are excluded from this automatic wrapping.
Business‑level exceptions are handled uniformly by defining an AppCode enum that implements StatusCode (e.g., APP_ERROR(2000, "业务异常")) and an APIException runtime exception that carries a code, a message, and a detailed error description. A second @RestControllerAdvice catches APIException and returns a ResultVo with the appropriate code and message.
Example usage shows throwing
new APIException(AppCode.ORDER_NOT_EXIST, "订单号不存在:" + orderId)and receiving a JSON response like
{"code":2003,"msg":"订单不存在","data":"订单号不存在:1998"}. The article also includes images illustrating the AOP interception flow and a health‑check endpoint that can be excluded from wrapping.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
