Java records
The Spring Boot plugin writes a Java record for each schema of your contract, with the Bean Validation annotations whose meaning matches the contract, for your own @Valid calls. The boundary checks the request with a generated structural validator.
Used by: Spring Boot.
Set up
Section titled “Set up”Nothing to add: the Gradle or Maven plugin writes the records under the schemas package of your package option. Spring Boot has no rules option.
What you get
Section titled “What you get”For the CreateTask schema of the overview:
import com.fasterxml.jackson.annotation.JsonInclude;import jakarta.validation.constraints.Min;import jakarta.validation.constraints.NotNull;import java.util.Optional;
@JsonInclude(JsonInclude.Include.NON_ABSENT)public record CreateTask(@NotNull String title, Optional<@Min(0) Long> effort) {The record carries these checks as Bean Validation annotations:
@NotNullon a required member, and@Validon a member that is a record.@Min,@Max,@DecimalMinand@DecimalMaxfor number bounds. Above,effortis@Min(0).@Sizeon a list, for its item count.
An optional member is an Optional. A uuid is a UUID and a date-time is an OffsetDateTime. An enum constant keeps its wire value with @JsonProperty.
Where the request is checked
Section titled “Where the request is checked”The request is not checked by these annotations. A generated structural validator in the Spring runtime checks the whole request before your controller runs: types, required members, unknown keys, number bounds, item counts and the string checks below. The annotations are for your own code: validate a record you build with @Valid and a Jakarta Validation provider, and the same bounds apply.
The record has no @Size on title, yet title must still have 1 to 200 characters. A string length is not an annotation, because Bean Validation counts a string in a way that differs from the contract. The structural validator checks:
- the length of a string, counted in code points as the contract counts it;
- a
pattern, aformatandmultipleOf.
When a check fails
Section titled “When a check fails”The structural validator reports every issue with its JSON pointer and its contract rule, and the server answers the 400 body.
Native or consistent rules
Section titled “Native or consistent rules”There is no choice: the checks are always the contract’s. Spring Boot has no rules option.
Limits
Section titled “Limits”The Spring Boot generator refuses by name a oneOf, a map schema and a file value (Generate fails).
- Spring Boot.
- The 400 body: When a check fails.