javavalidationspring-bootjsrjavax.validation

How to validate @NotBlank for simple string parameter directly with javax?


I want to validate GET / POST request for spring-boot controller classes with the javax.validation-api annotations.

For classes @Valid and @NotBlank for attributes of that class work perfectly.

The following works as expected:

public class Registration {
    @NotBlank
    private String name;
}

public ResponseEntity registration(@Valid @RequestBody Registration registration) {}

So now I only have a single string as parameter and would like to validate it.

Ist this possible?

The following doesn't work as expected (doesn't validate anything):

public ResponseEntity registration(@Valid @NotBlank String password) {}

It seems like such a simple requirement but I couldn't find anything on the internet or on Stackoverflow.


For reproduction I created a MWE (java 10, gradle project):

After starting the project call localhost:8080/registration?test= with POST for example with Postman. The parameter "test" will be empty but the method despite @NotBlank will be entered.

A POST call to localhost:8080/container fails as expected.

MweController.java

import javax.validation.constraints.*;

import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

@RestController
public class MweController {

    @CrossOrigin(origins = "http://localhost:3000")
    @PostMapping(value = "/registration")
    public ResponseEntity registration(@NotNull @NotBlank @NotEmpty String test) {
        System.out.println("Parameter: " + test);
        // This should return Bad Request but doesn't!
        return new ResponseEntity(HttpStatus.OK);
    }

    @CrossOrigin(origins = "http://localhost:3000")
    @PostMapping(value = "/container")
    public ResponseEntity container(@Valid Container test) {
        System.out.println("Parameter: " + test);
        // This returns Bad Request as expected
        return new ResponseEntity(HttpStatus.OK);
    }

    class Container {
        public Container(String test){
            this.test = test;
        }

        @NotBlank
        private String test;
    }

}

MweApplication.java

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

@SpringBootApplication
public class MweApplication {

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

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.0.M2'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.mwe'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 10

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-webflux')
}

Solution

  • Did you annotated your class with @Validated?

    For example:

    @Validated
    public class Controller {
    
       public ResponseEntity registration(@Valid @NotBlank String password) {}
    }