简体   繁体   English

来自javax.validation.constraints的注释不起作用(忽略)

[英]Annotations from javax.validation.constraints not working (ignored)

I'm trying to use javax constraints to pre-validate a request content before running the logic. 我正在尝试使用javax约束在运行逻辑之前预先验证请求内容。

I have tried any possible solution out there but still can't put javax annotations to work in a Spring boot. 我已经尝试了所有可能的解决方案,但仍然无法在Spring Boot中使用javax注释。

import javax.validation.constraints.Min
import javax.validation.constraints.Pattern

data class LoginRequest (
        @Credential //Custom constraint that works just fine
        val credential: String,

        @Min(value= 5)
        val password: String,

        @Pattern(regexp = Constants.Regex.DEVICE_ID_REGEX, message = "Invalid device ID")
        val device: String
): Serializable

This is a part of pom.xml 这是pom.xml的一部分

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
.
.
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.17.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator-annotation-processor</artifactId>
    <version>6.0.17.Final</version>
</dependency>

And this is the controller 这是控制器

import javax.validation.Valid


@PostMapping("/login")
    fun userLoginEndpoint(@Valid @RequestBody loginRequest: LoginRequest): ResponseEntity<User> {
        return authService.loginUser(loginRequest)
                ?.let{ ResponseEntity(it, HttpStatus.ACCEPTED)}
                ?: ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()
    }

Am I doing something wrong? 难道我做错了什么?

From @JBNizet comment 来自@JBNizet的评论

Replace @Min(value= 5) (and the other validation annotations too, of course) by @field:Min(value= 5) . @Min(value= 5) (当然还有其他验证注释) @field:Min(value= 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 javax.validation.constraints 的注释不起作用 - Annotations from javax.validation.constraints not working 结合自定义 ConstraintValidator 的 javax.validation.constraints 注释 - Annotations from javax.validation.constraints in combination with custom ConstraintValidator javax.validation.constraints中的转义序列无效 - Invalid escape sequencein javax.validation.constraints 原始布尔值的 javax.validation.constraints? - javax.validation.constraints for primitive boolean? 关注点重复javax.validation.constraints和javax.persistence.Column - Duplication of concerns javax.validation.constraints and javax.persistence.Column 以编程方式“关闭”bean验证(javax.validation.constraints) - “Turn off” bean validation programmatically (javax.validation.constraints) Java 中的 json 验证器 - 使用 javax.validation.constraints - json validator in Java - using javax.validation.constraints 如何为 `javax.validation.constraints` 注释添加自定义验证器? - how add custom validator for `javax.validation.constraints` annotation? Swagger软件包javax.validation.constraints不存在 - Swagger package javax.validation.constraints does not exist 是否可以将 spring boot @Value 与 javax.validation.constraints 结合使用? - Is it possible to combine spring boot @Value with javax.validation.constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM