java - 如何直接使用 javax 验证简单字符串参数的@NotBlank?

标签 java validation spring-boot jsr javax.validation

我想使用 javax.validation-api 注释验证对 spring-boot Controller 类的 GET/POST 请求。

对于 @Valid 类和 @NotBlank 类,该类的属性工作得很好。

以下按预期工作:

public class Registration {
    @NotBlank
    private String name;
}

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

所以现在我只有一个字符串作为参数并且想验证它。

这可能吗?

以下未按预期工作(未验证任何内容):

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

这似乎是一个如此简单的要求,但我在互联网或 Stackoverflow 上找不到任何东西。


为了重现,我创建了一个 MWE(java 10,gradle 项目):

启动项目后调用 localhost:8080/registration?test= 使用 POST 例如使用 Postman。参数“test”将为空,但将输入尽管有 @NotBlank 的方法。

localhost:8080/container 的 POST 调用按预期失败。

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);
    }
}

构建.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')
}

最佳答案

您是否使用 @Validated 注释了您的类(class)?

例如:

@Validated
public class Controller {

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

关于java - 如何直接使用 javax 验证简单字符串参数的@NotBlank?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058491/

相关文章:

java - 设置新屏幕时崩溃

java - Java中的可变变量

javascript - Selectize.js 和 jQuery 验证

linux - 为什么 Puppet 解析器在某些情况下失败,但对其他相同的情况有效?

java - Spring boot devtools 和 ActiveJDBC 集成

JavaMail - 抄送和密件抄送地址被忽略

java - 在下面的测试用例中获取 NotAMockException

java - 在 FormBean 中设置属性之前如何抛出异常或设置 ActionError

java - Spring Boot Microsoft AD 身份验证

spring-boot - JBAS015852 : Could not index class module-info. 类 - Spring Boot + Jboss 7.1.1