英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论