2021-12-30 @valid @validted 区别和使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-12-30 @valid @validted 区别和使用相关的知识,希望对你有一定的参考价值。
参考技术A @Valid是使用Hibernate validation的时候使用@Validated是只用Spring Validator校验机制使用
说明:java的JSR303声明了@Valid这类接口,而Hibernate-validator对其进行了实现
@Validation对@Valid进行了二次封装
所以必须添加 Hibernate validation 依赖
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
常用示例
@Data
public class TestVo
@NotBlank(message = "name不能为空")
@NotNull(message = "name不能为空123")
private String name;
@NotEmpty(message = "数据集不嫩为空")
@Valid //嵌套验证必须
private List<SubTestVo> datas;
@Data
public class SubTestVo
@Min(value = 5, message = "不能小于5")
private Integer age;
Post
@PostMapping("/test")
public void test(//@Valid 都行
@Validated
@RequestBody TestVo updateQo)
Get
//对象
@GetMapping("/test1")
public void test1(//@Valid 都行
@Validated
TestVo updateQo)
//多属性 需在Controller加 @Validated
@GetMapping("/test1")
public void test1(@Min(value = 5,message = "不小于5")
@RequestParam(value = "age")Integer age,
@NotNull(message = "不为空")
@RequestParam(value = "name")Integer name)
===================Validated 分组=============================
@Data
public class TestVo
@NotBlank(groups = Add.class,message = "name不能为空")
private String name;
@NotEmpty(groups = Query.class, message = "数据集不嫩为空")
@Valid
private List<SubTestVo> datas;
public interface Add extends Default ;
public interface Query extends Default ;
//都不验证
@PostMapping("/test")
public void test(//@Valid 分组效果也等同于Validated
@Validated
@RequestBody TestVo updateQo)
//只验证name 不验证 datas
@GetMapping("/test1")
public void test1(@Validated(TestVo.Add.class)
TestVo updateQo)
//只验证datas 不验证name
@GetMapping("/test2")
public void test2(@Validated(TestVo.Query.class)
TestVo updateQo)
========================自定义检验注解================================
1.自定义注解
@Target(FIELD)
@Retention(RUNTIME)
@Constraint(validatedBy = RegionValidator.class)//自定义的校验类
@Documented
public @interface Region
String message()default "Region 值不在可选范围内";
Class[] groups()default ;
Class[] payload()default ;
2.定义检验类
@Configuration
public class RegionValidatorimplements ConstraintValidator
@Override
public boolean isValid(String value, ConstraintValidatorContext context)
HashSet regions =new HashSet<>();
regions.add("China");
regions.add("China-Taiwan");
regions.add("China-HongKong");
return regions.contains(value);
3.在字段上使用
@Region(message= "区域不合法")
private String regionName;
总结: 保持一致性上可以选择 在Controller 都使用 @Validated 在入参 校验;若单独属性 需要在类上加
分组需求时, 不写或漏写分组, 默认不校验
LWC 74: 794. Valid Tic-Tac-Toe State
LWC 74: 794. Valid Tic-Tac-Toe State
传送门:794. Valid Tic-Tac-Toe State
Problem:
A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
The board is a 3 x 3 array, and consists of characters ” “, “X”, and “O”. The ” ” character represents an empty square.
Here are the rules of Tic-Tac-Toe:
Players take turns placing characters into empty squares (” “).
The first player always places “X” characters, while the second player always places “O” characters.
“X” and “O” characters are always placed into empty squares, never filled ones.
The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Example 1:
Input: board = [“O “, ” “, ” “]
Output: false
Explanation: The first player always plays “X”.
Example 2:
Input: board = [“XOX”, ” X “, ” “]
Output: false
Explanation: Players take turns making moves.
Example 3:
Input: board = [“XXX”, ” “, “OOO”]
Output: false
Example 4:
Input: board = [“XOX”, “O O”, “XOX”]
Output: true
Note:
- board is a length-3 array of strings, where each string board[i] has length 3.
- Each board[i][j] is a character in the set ” “, “X”, “O”.
题意:
检测当前局面是否为合法局面。
思路:
1. 由规则可知,”X”一定最先开始,所以当前局面存在”O”的个数大于”X”的个数为非法。
2. 其次,由于”X”和”O”轮流,因此,当前局面中”X”的个数要么和”O”相等,要么比”O”多一。
3. “O”在当前局面赢得比赛的情况下,上一轮的”X”一定不能赢得局面。
4. “O”在当前局面赢得比赛的情况下,上一轮的”X”没有赢得局面时,合法局面必须满足”O”的个数等于”X”的个数。
5. “X”在当前局面赢得比赛的情况下,意味着上一轮”O”没有赢得局面,合法局面下,”X”的个数正好比”O”的个数多一。
Java版本:
public boolean validTicTacToe(String[] board)
int x_count = 0;
int o_count = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (board[i].charAt(j) == 'X') x_count++;
else if (board[i].charAt(j) == 'O') o_count++;
char[][] map = new char[3][3];
for (int i = 0; i < 3; ++i)
map[i] = board[i].toCharArray();
if (o_count > x_count || x_count - o_count > 1) return false;
if (check_win_positions(map, 'O'))
if (check_win_positions(map, 'X'))
return false;
return o_count == x_count;
if (check_win_positions(map, 'X') && x_count != o_count + 1) return false;
return true;
boolean check_win_positions(char[][] map, char player)
for (int i = 0; i < 3; ++i)
if (map[i][0] == map[i][1] && map[i][1] == map[i][2]
&& map[i][2] == player)
return true;
for (int j = 0; j < 3; ++j)
if (map[0][j] == map[1][j] && map[1][j] == map[2][j]
&& map[2][j] == player)
return true;
if (map[0][0] == map[1][1] && map[1][1] == map[2][2] && map[2][2] == player ||
map[0][2] == map[1][1] && map[1][1] == map[2][0] && map[2][0] == player)
return true;
return false;
Python版本:
class Solution(object):
def check_win_positions(self, board, player):
#Check the rows
for i in range(len(board)):
if board[i][0] == board[i][1] == board[i][2] == player:
return True
#Check the columns
for i in range(len(board)):
if board[0][i] == board[1][i] == board[2][i] == player:
return True
#Check the diagonals
if board[0][0] == board[1][1] == board[2][2] == player or \\
board[0][2] == board[1][1] == board[2][0] == player:
return True
return False
def validTicTacToe(self, board):
"""
:type board: List[str]
:rtype: bool
"""
x_count, o_count = 0, 0
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == "X":
x_count += 1
elif board[i][j] == "O":
o_count += 1
if o_count > x_count or x_count-o_count>1:
return False
if self.check_win_positions(board, 'O'):
if self.check_win_positions(board, 'X'):
return False
return o_count == x_count
if self.check_win_positions(board, 'X') and x_count!=o_count+1:
return False
return True
以上是关于2021-12-30 @valid @validted 区别和使用的主要内容,如果未能解决你的问题,请参考以下文章
2021-12-30:分裂问题。 一个数n,可以分裂成一个数组[n/2, n%2, n/2], 这个数组中哪个数不是1或者0,就继续分裂下去。 比如 n = 5,一开始分裂成[2, 1, 2], [2
2021-12-30:分裂问题。 一个数n,可以分裂成一个数组[n/2, n%2, n/2], 这个数组中哪个数不是1或者0,就继续分裂下去。 比如 n = 5,一开始分裂成[2, 1, 2], [2