首页 > 解决方案 > Hibernate Validator with Hibernate Dynamic Models (No Entity Class)

问题描述

我正在使用休眠动态模型来创建 CRUD。我想知道如何验证 http 请求 (@Valid) 注释,因为我没有任何实体或 DTO。

@PostMapping("/signup")
public ResponseEntity<RestResponse> signup(@Valid @RequestBody Map<String, Object> user) {
    var restResponse = new RestResponse<>();

    restResponse.setStatusCode(HttpStatus.OK.value());
    restResponse.setResponse(user);

    return ResponseEntity.status(HttpStatus.OK)
            .body(restResponse);
}

标签: spring-boothibernatehibernate-validator

解决方案


你不能,因为没有用于 bean 验证注释的 java 类,这是 bean 验证的唯一基础。也许您应该切换到适当的基于类的模型,这也将使您的应用程序更容易理解和调试


推荐阅读