Skip to content

Spring Boot REST API Validation and Large Payload Handling

Spring Boot uses Bean Validation (JSR-380 / Jakarta Validation).

spring-boot-starter-validation
public class Product {
@NotNull
private String name;
@Min(1)
private double price;
}
@PostMapping("/products")
public Product createProduct(@Valid @RequestBody Product product) {
return productService.save(product);
}
Annotation Purpose
@NotNull Cannot be null
@NotBlank Cannot be blank
@Size Restricts length
@Min, @Max Numeric range
@Email Valid email format
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
@PostMapping("/upload")
public String uploadFile(@RequestParam MultipartFile file) {
fileService.save(file);
return "File uploaded successfully";
}

Request content type: multipart/form-data.

Use InputStream instead of loading the entire payload into memory.

Request:

GET /products?page=1&size=20

Repository usage:

Page<Product> products =
productRepository.findAll(PageRequest.of(0, 10));

Add sorting with Sort, either standalone or combined with pagination:

Sort sort = Sort.by("salary").descending();
productRepository.findAll(sort);
Pageable pageable =
PageRequest.of(0, 10, Sort.by("salary").descending());
productRepository.findAll(pageable);
server.compression.enabled=true
  • @Valid combined with Bean Validation annotations (@NotNull, @Size, @Email, etc.) validates incoming payloads declaratively, without manual if-checks in the controller.
  • For large payloads, prefer streaming (InputStream) over loading the full body into memory.
  • Page<T> + PageRequest bound results at the repository layer, complementing the ?page=&size= API-level pagination convention.
  • Enabling response compression (server.compression.enabled=true) reduces payload size over the wire for large JSON responses.