验证 @RequestBody 布尔属性(应为真或假)

huangapple go评论79阅读模式
英文:

Validating @RequestBody boolean attribute (to be true or false)

问题

I read a lot of topics but did not find this information in any of them. If by any chance I missed it, I would really appreciate it if you would guide me to it 验证 @RequestBody 布尔属性(应为真或假) .

我阅读了很多话题,但在其中没有找到这些信息。如果我不小心错过了,我会非常感激您指引我 验证 @RequestBody 布尔属性(应为真或假)

I am using Spring Boot and got a User model which has a boolean field - active. I am using a boolean because I read that it is a bad practice to use Boolean for 3 state checks.

我正在使用Spring Boot,拥有一个User模型,其中有一个boolean字段 - active。我使用boolean是因为我读到使用Boolean进行3个状态检查是不好的做法。

The problem is that when I send a RequestBody from Postman and if I leave the field active empty, it defaults it to false. I read about things like @NotNull annotation with @Validation and @Valid check on Body, but that is just not an option with boolean.

问题是,当我从Postman发送一个RequestBody并且将active字段留空时,它会默认为false。我读到了关于使用@NotNull注解与@Validation@Valid在Body上进行检查的内容,但对于布尔类型来说,这不是一个选项。

Main question: Is it possible to do something like (@AssertTrue or @AssertFalse) on a boolean property? and if not, how is it possible to make sure this property is not empty in the RequestBody then (not using Boolean)?

主要问题:是否可以在布尔属性上执行类似于(@AssertTrue@AssertFalse)的操作?如果不能,那么如何确保在RequestBody中这个属性不为空(而不使用Boolean)?

EDIT: I just thought that code might not be necessary for this question as it is quite straightforward, here you go, Controller method:

编辑:我只是觉得代码可能对这个问题不是必要的,因为这个问题相当简单,以下是Controller方法:

@PutMapping()
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity modifyUser(@RequestBody User userBody)
throws NotFoundException {

User class property:

@Column(name = "ACTIVE")
private boolean active;

英文:

so I read a lot of Topics but did not find this information in any of them. If by any chance i missed it I would really appreciate if you would guide me to it 验证 @RequestBody 布尔属性(应为真或假) .

I am using SringBoot and got a User model which has a boolean field - active. I am using a boolean because i read that it is a bad practice to use Boolean for 3 state checks.

The problem is that when i send a RequestBody from Postman and if i leave field active empty it defaults it to false. I read about things like @NotNull annotation with @Validation and @Valid check on Body but that is just not an option with boolean.

Main question: Is it possible to do something like (@AssertTrue or @AssertFalse) on a boolean property? and if not, how is it possible to make sure this property is not empty in the RequestBody then (not using Boolean)?

EDIT: I just thought that code might not be necessary for this question as it is quite straight forward, here you go, Controller method:

@PutMapping()
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<User> modifyUser(@RequestBody User userBody)
        throws NotFoundException {

User class property:

    @Column(name = "ACTIVE")
private boolean active;

答案1

得分: 1

boolean数据类型是二进制表示(true/false)。Null值既不是true也不是false。

默认情况下,boolean的值为false。
参考原始数据类型

如果您想基于发送的用户数据执行一些验证,您将需要将数据类型创建为包装器Boolean。这将取决于您希望进行的解释方式。

  1. 如果为null,您是否要计算某个值并设置字段?
  2. 您是否希望在数据库中将值存储为null?

但理想情况下,如果字段是布尔值,其值应该是true或false。

英文:

boolean data type is a binary representation (true/false). Null value is neither true or false.

By default the value of boolean is false.
Reference Primitive Data Types

If you want to do some validation based on the user data that is sent you would have to create the data type as wrapper Boolean.
It would depend on the scenario of what you would the interpretation to be.

  1. If null will you be calculating some value and setting the field?
  2. Do you want to store the value as null in the database?

But ideally if a field is boolean the value should be either true or false.

答案2

得分: 0

所以最终结果中,我仍然需要使用布尔值,因此我随后能够在active字段上添加@NotNull注释。不幸的是,仍然无法找到如何使用布尔值来执行此操作的答案...感谢回复!

英文:

So in the final result i still had to use Boolean, so i was then able to add @NotNull annotation to active field. Sadly still was not able to find answer on how to do it with boolean... Thanks for the reply!

huangapple
  • 本文由 发表于 2020年8月12日 19:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63375343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定