Spring boot Springboot验证在Kotlin中不起作用
我正在使用不同的RESTAPI创建一个新的Springboot应用程序。我试图对输入参数使用验证,但它不起作用。即使有注释,我也能够在名称字段中插入空白内容。有人知道会出什么问题吗 我已经搜索了很多关于这一点,我看到很多东西说要做我所做的事情。我已经尝试使用@field:NotNull和@field:Size(min=1),我也遇到了同样的问题 我的控制器:Spring boot Springboot验证在Kotlin中不起作用,spring-boot,validation,kotlin,Spring Boot,Validation,Kotlin,我正在使用不同的RESTAPI创建一个新的Springboot应用程序。我试图对输入参数使用验证,但它不起作用。即使有注释,我也能够在名称字段中插入空白内容。有人知道会出什么问题吗 我已经搜索了很多关于这一点,我看到很多东西说要做我所做的事情。我已经尝试使用@field:NotNull和@field:Size(min=1),我也遇到了同样的问题 我的控制器: @PostMapping @Operation(summary = "Insert a new client"
@PostMapping
@Operation(summary = "Insert a new client")
fun insert(@Valid @RequestBody clientDTO : ClientInputDTO) : ResponseEntity<Void> {
val client = clientService.fromInputDTO(clientDTO)
val personInserted = personService.insert(client.person)
client.person.addresses.forEach {
it.person=personInserted
addressService.insert(it)
}
val clientInserted = clientService.insert(client)
val uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(clientInserted.id).toUri()
return ResponseEntity.created(uri).build()
}
@PostMapping
@操作(summary=“插入新客户”)
有趣的插入(@Valid@RequestBody clientDTO:ClientInputDTO):ResponseEntity{
val client=clientService.fromInputDTO(clientDTO)
val personInserted=personService.insert(client.person)
client.person.addresses.forEach{
it.person=插入的个人
addressService.insert(it)
}
val clientInserted=clientService.insert(客户端)
val uri=ServletUriComponentsBuilder.fromCurrentRequest().path(“/{id}”).buildAndExpand(clientInserted.id).toUri()
return ResponseEntity.created(uri.build)()
}
我的DTO:
class ClientInputDTO(@JsonProperty("name")
@field:NotEmpty(message="name validation test")
val name: String,
@JsonProperty("person_type")
val personType: String,
@JsonProperty("document")
val document: Long,
@JsonProperty("rg")
val rg: String?,
@JsonProperty("addresses")
val addresses: List<AddressInputDTO>
)
类ClientInputDTO(@JsonProperty(“名称”)
@字段:NotEmpty(message=“名称验证测试”)
val name:String,
@JsonProperty(“个人类型”)
val personType:String,
@JsonProperty(“文件”)
val文件:长,
@JsonProperty(“rg”)
val rg:字符串?,
@JsonProperty(“地址”)
val地址:列表
)
根据从Spring Boot 2.3开始,您需要明确添加Spring Boot starter验证依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
org.springframework.boot
弹簧启动启动器验证
您能否确认您对您的项目有此依赖关系,以便我们进一步调查?根据Spring Boot 2.3开始,您需要明确添加Spring Boot starter验证依赖关系:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
org.springframework.boot
弹簧启动启动器验证
您能否确认您对您的项目有这种依赖性,以便我们进一步调查