spring-boot - Kotlin Spring Boot Webflux 使用 @Valid 注解验证 @RequestBody

标签 spring-boot rest kotlin spring-webflux

我在 Spring Boot 中使用 Webflux 有一个简单的 REST API。 我想对 POST 请求中的请求正文使用基于简单注释的验证。

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)

}

期望:当我提供一个带有空字符串作为标题和/或超过 10 个字符的描述的 POST 请求时,我不应该得到 201 CREATED 作为响应。

Checking with Postman

如您所见,我收到了 201 CREATED 作为响应。

有人看出我哪里出错了吗???

最佳答案

您需要在 Controller 中进行一些更改(@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 - Kotlin Spring Boot Webflux 使用 @Valid 注解验证 @RequestBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70067165/

相关文章:

安卓 Kotlin : Required Context but found String

gradle -> 无法应用插件 [id 'kotlin'] > 对于输入字符串 : ""

javascript - Microsoft Edge 浏览器缓存中的 RESTful Angular 应用

spring-boot - 在 ConsumerConfig 中添加 kafka autoStartUp false

java - 在请求正文中使用时配置属性不会 Autowiring ?

java - 让我的 Spring 测试切片扫描单个类而不是整个包

java - 发布/记录 Spring-REST API

rest - 使用 Windows Live ID 访问 Windows Azure 服务管理 API

android - 如何观察PagedList数据?

database - 使用多个数据库和 Liquibase 时的最佳 Hibernate 主键生成类型