Skip to content
Veltoryx Technologies
ProLesson 3.2Intermediate2 min readUpdated 31 October 2026

REST APIs, Validation & Global Exception Handling

Build robust REST APIs with Jakarta Validation, @RestControllerAdvice, and RFC 7807 Problem Details.

In a distributed microservices ecosystem, client applications (web frontends, iOS/Android apps, or other microservices) need consistent, predictable error responses.

If Service A returns error JSON with { "err": "not found" }, Service B returns { "message": "missing" }, and Service C dumps an unhandled HTML stack trace (HTTP 500 NullPointerException), client developers will struggle to parse errors reliably.

In Spring Boot 3, the industry standard for REST API errors is RFC 7807 Problem Details.

01Implementing the Order Controller

java
package com.veltoryx.order.controller;

import com.veltoryx.order.controller.dto.CreateOrderRequest;
import com.veltoryx.order.controller.dto.OrderResponse;
import com.veltoryx.order.service.OrderService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.UUID;

@RestController
@RequestMapping("/api/v1/orders")
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping
    public ResponseEntity<OrderResponse> createOrder(@Valid @RequestBody CreateOrderRequest request) {
        OrderResponse created = orderService.createOrder(request);
        URI location = URI.create("/api/v1/orders/" + created.orderId());
        return ResponseEntity.created(location).body(created);
    }

    @GetMapping("/{id}")
    public ResponseEntity<OrderResponse> getOrderById(@PathVariable UUID id) {
        OrderResponse order = orderService.getOrderById(id);
        return ResponseEntity.ok(order);
    }
}

Notice:

  • @Valid: Triggers Jakarta Bean Validation before the controller method executes.
  • ResponseEntity.created(location): Returns HTTP 201 Created with a Location header pointing to the new resource.

Loading the rest of the lesson…