对于在Spring MVC中实现的简单RESTful JSON api,我可以使用Bean Validation(JSR-303)来验证传递给处理程序方法的路径变量吗?
例如:
@RequestMapping(value = "/number/{customerNumber}")
@ResponseBody
public ResponseObject searchByNumber(@PathVariable("customerNumber") String customerNumber) {
...
}
Run Code Online (Sandbox Code Playgroud)
在这里,我需要使用Bean验证来验证customerNumber变量的长度.这可能是Spring MVC v3.xx吗?如果没有,这种验证的最佳方法是什么?
谢谢.
如何在spring中验证我的路径变量.我想验证id字段,因为它只有单个字段我不想移动到Pojo
@RestController
public class MyController {
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity method_name(@PathVariable String id) {
/// Some code
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在路径变量中添加验证,但它仍然无效
@RestController
@Validated
public class MyController {
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity method_name(
@Valid
@Nonnull
@Size(max = 2, min = 1, message = "name should have between 1 and 10 characters")
@PathVariable String id) {
/// Some code
}
}
Run Code Online (Sandbox Code Playgroud)