Reference: rest-api
Source file: .claude/java-architect/references/rest-api.md
REST API Patterns
pom.xml additions
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web</artifactId>
</dependency>
Controller
@RestController
@RequestMapping ( "/api/v1/orders" )
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService ;
@GetMapping ( "/{id}" )
public ResponseEntity < OrderDto > getOrder ( @PathVariable UUID id ) {
return orderService . findById ( id )
. map ( ResponseEntity :: ok )
. orElse ( ResponseEntity . notFound (). build ());
}
@PostMapping
@ResponseStatus ( HttpStatus . CREATED )
public OrderDto createOrder ( @Valid @RequestBody CreateOrderRequest request ) {
return orderService . create ( request );
}
}
WebFlux (Reactive) Controller
@RestController
@RequestMapping ( "/api/v1/orders" )
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService ;
@GetMapping ( "/{id}" )
public Mono < ResponseEntity < OrderDto >> getOrder ( @PathVariable UUID id ) {
return orderService . findById ( id )
. map ( ResponseEntity :: ok )
. defaultIfEmpty ( ResponseEntity . notFound (). build ());
}
@PostMapping
@ResponseStatus ( HttpStatus . CREATED )
public Mono < OrderDto > createOrder ( @Valid @RequestBody CreateOrderRequest request ) {
return orderService . create ( request );
}
}
Global Exception Handler
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler ( EntityNotFoundException . class )
public ProblemDetail handleNotFound ( EntityNotFoundException ex ) {
ProblemDetail problem = ProblemDetail . forStatusAndDetail (
HttpStatus . NOT_FOUND , ex . getMessage ());
problem . setProperty ( "timestamp" , Instant . now ());
return problem ;
}
@ExceptionHandler ( MethodArgumentNotValidException . class )
public ProblemDetail handleValidation ( MethodArgumentNotValidException ex ) {
ProblemDetail problem = ProblemDetail . forStatusAndDetail (
HttpStatus . BAD_REQUEST , "Validation failed" );
problem . setProperty ( "errors" , ex . getBindingResult ()
. getFieldErrors (). stream ()
. map ( e -> e . getField () + ": " + e . getDefaultMessage ())
. toList ());
return problem ;
}
}
OpenAPI Config
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI () {
return new OpenAPI ()
. info ( new Info ()
. title ( "Demo Service API" )
. version ( "1.0.0" ));
}
}
Quick Reference
Annotation
Purpose
@RestController
Marks class as REST controller (combines @Controller + @ResponseBody)
@RequestMapping
Base path for all methods in the controller
@GetMapping / @PostMapping
HTTP method-specific mappings
@PathVariable
Bind URI segment to parameter
@RequestBody
Deserialize request body
@Valid
Trigger Bean Validation on the bound object
@ResponseStatus
Set default HTTP status for the method
ProblemDetail
RFC 7807-compliant error response
@RestControllerAdvice
Global exception handling across all controllers