Sub topic 10
Documenting responses and error payloads
This chapter assumes you have a working springdoc setup, know @Operation and @Schema, and can write a Spring @RestControllerAdvice.
What springdoc documents by default
For each operation, springdoc infers one success response from the method's return type. A GET that returns Book gets a 200 response with the Book schema. A void method gets a 200 with no body. Error responses are not inferred from your code, with one exception covered below. You declare them yourself, or the document shows only the success case.
Declaring responses with @ApiResponse
@ApiResponse pairs a status code with a description and, optionally, a body schema. The content attribute controls the body:
- Omit
contentand springdoc reuses the method's return type. - Use
content = @Contentwith no arguments to say "no body". - Use
@Content(schema = @Schema(implementation = ...))to name a specific payload type.
Modeling the error payload
Define one error type and reuse it everywhere. Annotate it like any other model:
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "Error returned for failed requests")
public record ErrorResponse(
@Schema(description = "HTTP status code", example = "404")
int status,
@Schema(description = "Short error name", example = "Not Found")
String error,
@Schema(description = "Explanation for the client", example = "Book 42 does not exist")
String message) {
}Now point the error responses at it:
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/books")
public class BookController {
private final BookService service;
public BookController(BookService service) {
this.service = service;
}
@Operation(summary = "Get a book by id")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Book found"),
@ApiResponse(responseCode = "404", description = "Book not found",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/{id}")
public Book getBook(@PathVariable long id) {
return service.findById(id);
}
}The 200 entry has no content, so springdoc fills in the Book schema from the return type.
Letting @RestControllerAdvice do the work
Repeating the 404 on every method is tedious. springdoc scans @RestControllerAdvice classes. For each @ExceptionHandler method that has @ResponseStatus, it adds a response to every operation, using the handler's return type as the body schema.
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
class BookNotFoundException extends RuntimeException {
BookNotFoundException(long id) {
super("Book " + id + " does not exist");
}
}
@RestControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(BookNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(BookNotFoundException ex) {
return new ErrorResponse(404, "Not Found", ex.getMessage());
}
}Advice-based documentation applies to all operations. A 404 will therefore also appear on endpoints that never return one. Declare it explicitly where accuracy matters.
Checking the result
Open /v3/api-docs and find your operation under paths. Each status code should appear under responses, and the error body should reference #/components/schemas/ErrorResponse. In Swagger UI the same entries appear in the Responses section, with the example values from your @Schema annotations.
Exercise
A method returns Book and declares @ApiResponse(responseCode = "204", description = "Deleted", content = @Content). What body schema does the 204 entry show, and why?
Show answer
None. An empty @Content means "no body", so springdoc does not fall back to the return type. Omitting content would make springdoc reuse the Book schema.