Springboot Kotlin @Pattern注解被忽略了

huangapple go评论78阅读模式
英文:

Springboot Kotlin @Pattern annotation ignored

问题

我非常渴望解决这个问题。我只想用@Pattern注解验证输入参数,但它完全忽略了它。
代码接受所有的请求。

/hello?name=as2ds -> 通过
/hello?name=sadasdasdasda -> 通过

请指导我。

import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid
import javax.validation.constraints.Pattern

@RestController
@Validated
class HelloController {

    @GetMapping("/hello")
    fun getHello(@Valid @RequestParam("name") @Pattern(regexp = "^[a-z]{4}$") name : String) : String {
        return "Hello $name!"
    }
}
英文:

I am really desperate to solve this issue. All I want is to validate the input parameter with @Pattern annotation, but it simply ignores it.
The code accepts all the requests coming through.

/hello?name=as2ds -> PASSED
/hello?name=sadasdasdasda -> PASSED

Please guide me.

import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid
import javax.validation.constraints.Pattern

@RestController
@Validated
class HelloController {

    @GetMapping("/hello")
    fun getHello(@Valid @RequestParam("name") @Pattern(regexp = "^[a-z]{4}$") name : String) : String {
        return "Hello $name!"
    }
}

答案1

得分: 1

感谢 @Alexej Timonin 的帮助,问题已经解决。
他的评论解决了这个问题。

> Spring 3.x 从 javax.* 改为 jakarta.*(您正在使用 spring-boot-starter-validation 3.1.1)。将您的 Valid 和 Pattern 导入更改为 jakarta.validation.Valid 和 jakarta.validation.constraints.Pattern。

英文:

Thanks to @Alexej Timonin the problem has been solved.
His comment solved the probelm

> Spring 3.x went from javax.* to jakarta.* (You are using
> spring-boot-starter-validation 3.1.1). Change your Valid and Pattern
> imports to jakarta.validation.Valid and
> jakarta.validation.constraints.Pattern

huangapple
  • 本文由 发表于 2023年7月27日 16:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定