有没有一种方法可以查询与任何特定内容无关的字符串请求。我知道带有 @Validation 的 Spring 注释,但是当查询未绑定(bind)到对象时我正在寻找一些东西。
例如,请求是一个字符串值,然后某些业务逻辑会处理该字符串。该字符串不应为空且长度至少为 3 个字符。是否可以在哪里检查并发送适当的错误响应,还是必须手动执行?
类似于在 Express/Node.js 中使用 Joi(注意架构是如何当场创建而不是引用对象):
app.post('/api/courses', (req, res) => {
const schema = {
name: Joi.string().min(3).required()
}
const result = Joi.validate(req.body, schema);
//check result if error, otherwise continue
}
这是我正在寻找的 Spring/Java 表示,
@GetMapping
public String getSumOfWord(@RequestParam String word){
//Validate here that word is non-empty and > 3 characters
//Buisness logic (i.e. calculate letter total where a = 1, b = 2 etc.)
}
最佳答案
您可以使用 @Size 和 @RequestParam 进行尺寸验证
@GetMapping
public String getSumOfWord(@RequestParam @Size(min= 1, max = 3 , message = "word must be less than 3 characters") String word){
}
关于java - Spring查询字符串简单验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52021714/