Languages
[Edit]
EN

Spring Boot 2 - min and max values validation in @RequestParam

3 points
Created by:
Root-ssh
179400

This short article will show how to validate minimal and maximal allowed values in request parameters in Spring Boot 2 controllers.

Hint: to use min-max validation it is required to use integer numbers as request parameters.

Example preview:

Min and max values validation in @RequestParam in Spring Boot 2.
Min and max values validation in @RequestParam in Spring Boot 2.

HomeController.java file:

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

@Controller
@Validated  // <------------------------------------------------------------ REQUIRED
public class HomeController {

    @RequestMapping(
            value = "/example"
    )
    @ResponseBody
    public String index(
            @RequestParam @Min(1)           Integer pageNumber,  // <------- e.g. @Min        (REQUIRED @RequestParam)
            @RequestParam @Min(1) @Max(100) Integer pageSize     // <------- e.g. @Min + @Max (REQUIRED @RequestParam)
    ) {
        return "pageNumber=" + pageNumber + " pageSize=" + pageSize;
    }
}

 

DemoConfiguration.java file:

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

@Configuration
public class DemoConfiguration {

    @Bean  // <------------------------------------------------------------- REQUIRED
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }
}

 

DemoApplication.java file:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

 

pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath/>
	</parent>

	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>                                                   <!-- -- REQUIRED -->
			<groupId>org.springframework.boot</groupId>                <!-- -- REQUIRED -->
			<artifactId>spring-boot-starter-validation</artifactId>    <!-- -- REQUIRED -->
			<version>2.7.1</version>                                   <!-- -- REQUIRED -->
		</dependency>                                                  <!-- -- REQUIRED -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

References

  1. Spring Boot Starter Validation - Maven Repos
  2. Bean Validation API - Maven Repos
  3. Jakarta Bean Validation

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join