Kotlin Spring 引导 Webflux 使用 @Valid 注释验证 @RequestBody
Kotlin Spring Boot Webflux validate @RequestBody with @Valid annotation
我在 Spring Boot with Webflux 中有一个简单的 REST API。
我想在我的 POST 请求中对请求 body 使用简单的基于注释的验证。
RecipeModel.kt
package com.example.reactive.models
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import org.springframework.stereotype.Component
import javax.validation.constraints.Max
import javax.validation.constraints.NotBlank
@Table("recipes")
data class Recipe (
@Id
val id: Long?,
@NotBlank(message = "Title is required")
val title: String,
@Max(10, message = "Description is too long")
val description: String?,
)
RecipeRepo.kt
package com.example.reactive.repositories
import com.example.reactive.models.Recipe
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
@Repository
interface RecipeRepo : ReactiveCrudRepository<Recipe, Long>
RecipeController.kt
package com.example.reactive.controllers
import com.example.reactive.models.Recipe
import com.example.reactive.models.RecipeMapper
import com.example.reactive.repositories.RecipeRepo
import com.example.reactive.services.RecipeService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import javax.validation.Valid
@RestController
@RequestMapping("/recipes")
class RecipeController(val recipeService : RecipeService) {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createRecipe(@RequestBody payload: @Valid Recipe): Mono<Recipe> =
recipeService.createRecipe(payload)
}
RecipeService.kt
package com.example.reactive.services
import com.example.reactive.models.Recipe
import com.example.reactive.models.RecipeMapper
import com.example.reactive.repositories.RecipeRepo
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@Service
class RecipeService(val recipeRepo: RecipeRepo, val recipeMapper: RecipeMapper) {
fun createRecipe(recipe: Recipe): Mono<Recipe> = recipeRepo.save(recipe)
}
预期:当我提供一个 POST 请求,其中包含一个空字符串作为标题 and/or 一个超过 10 个字符的描述时,我不应该得到一个 201 CREATED 作为响应。
Checking with Postman
如您所见,我收到了 201 CREATED 作为响应。
有没有人看到我哪里弄错了???
您需要对控制器进行一些更改(@Valid
放置):
@RestController
@RequestMapping("/recipes")
class RecipeController(val recipeService : RecipeService) {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createRecipe(@RequestBody @Valid payload: Recipe): Mono<Recipe> =
recipeService.createRecipe(payload)
}
还有模型本身(您需要使用 @field:
):
@Table("recipes")
data class Recipe (
@field:Id
val id: Long?,
@field:NotBlank(message = "Title is required")
val title: String,
@field:Max(10, message = "Description is too long")
val description: String?,
)
我在 Spring Boot with Webflux 中有一个简单的 REST API。 我想在我的 POST 请求中对请求 body 使用简单的基于注释的验证。
RecipeModel.kt
package com.example.reactive.models
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import org.springframework.stereotype.Component
import javax.validation.constraints.Max
import javax.validation.constraints.NotBlank
@Table("recipes")
data class Recipe (
@Id
val id: Long?,
@NotBlank(message = "Title is required")
val title: String,
@Max(10, message = "Description is too long")
val description: String?,
)
RecipeRepo.kt
package com.example.reactive.repositories
import com.example.reactive.models.Recipe
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
@Repository
interface RecipeRepo : ReactiveCrudRepository<Recipe, Long>
RecipeController.kt
package com.example.reactive.controllers
import com.example.reactive.models.Recipe
import com.example.reactive.models.RecipeMapper
import com.example.reactive.repositories.RecipeRepo
import com.example.reactive.services.RecipeService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import javax.validation.Valid
@RestController
@RequestMapping("/recipes")
class RecipeController(val recipeService : RecipeService) {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createRecipe(@RequestBody payload: @Valid Recipe): Mono<Recipe> =
recipeService.createRecipe(payload)
}
RecipeService.kt
package com.example.reactive.services
import com.example.reactive.models.Recipe
import com.example.reactive.models.RecipeMapper
import com.example.reactive.repositories.RecipeRepo
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@Service
class RecipeService(val recipeRepo: RecipeRepo, val recipeMapper: RecipeMapper) {
fun createRecipe(recipe: Recipe): Mono<Recipe> = recipeRepo.save(recipe)
}
预期:当我提供一个 POST 请求,其中包含一个空字符串作为标题 and/or 一个超过 10 个字符的描述时,我不应该得到一个 201 CREATED 作为响应。
Checking with Postman
如您所见,我收到了 201 CREATED 作为响应。
有没有人看到我哪里弄错了???
您需要对控制器进行一些更改(@Valid
放置):
@RestController
@RequestMapping("/recipes")
class RecipeController(val recipeService : RecipeService) {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createRecipe(@RequestBody @Valid payload: Recipe): Mono<Recipe> =
recipeService.createRecipe(payload)
}
还有模型本身(您需要使用 @field:
):
@Table("recipes")
data class Recipe (
@field:Id
val id: Long?,
@field:NotBlank(message = "Title is required")
val title: String,
@field:Max(10, message = "Description is too long")
val description: String?,
)