Spring REST 服务控制器未被@PathVariable 和@Valid 验证 [英] Spring REST Service Controller not being validate by @PathVariable and @Valid
问题描述
@Controller
@EnableWebMvc
@Validated
public class ChildController extends ParentController<InterfaceController> implements InterfaceController{
@Override
@RequestMapping(value = "/map/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
@ResponseStatus( HttpStatus.OK)
@ResponseBody
public List<Friends> getAllFriendsByName(
@Valid
@Size(max = 2, min = 1, message = "name should have between 1 and 10 characters")
@PathVariable("name") String name,
@RequestParam(value="pageSize", required=false) String pageSize,
@RequestParam(value="pageNumber", required=false) String pageNumber,
HttpServletRequest request) throws BasicException {
//Some logic over here;
return results;
}
@ExceptionHandler(value = { ConstraintViolationException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public String handleResourceNotFoundException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
StringBuilder strBuilder = new StringBuilder();
for (ConstraintViolation<?> violation : violations ) {
strBuilder.append(violation.getMessage() + "\n");
}
return strBuilder.toString();
}
我正在尝试对 spring 请求参数进行非常基本的验证,但它似乎没有调用异常处理程序,有人可以指出我正确的方向
Hi, I am trying to do pretty basic validation for a spring request parameter but it just doesn't seem to call the Exception handler, could someone point me into the right direction
附言我不断收到 NoHandlerFoundException
P.S. I keep getting NoHandlerFoundException
推荐答案
Spring 不支持使用 @Valid
验证 @PathVariable
.但是,您可以在处理程序方法中进行自定义验证,或者如果您坚持使用 @Valid
然后编写自定义编辑器,将路径变量值转换为对象,使用 JSR 303 bean 验证,然后使用 @对该对象有效.这可能真的有效.
Spring doesn't support @PathVariable
to be validated using @Valid
. However, you can do custom validation in your handler method or if you insist on using @Valid
then write a custom editor, convert your path variable value to an object, use JSR 303 bean validation and then use @Valid on that object. That might actually work.
这是第三种方法.您实际上可以欺骗 spring 将您的路径变量视为模型属性,然后对其进行验证.1. 为您的路径变量编写自定义验证器2. 为您的路径变量构造一个 @ModelAttribute
,然后使用 @Validator
(是的,不是 @Valid
,因为它不允许您指定验证器)在该模型属性上.
Here's a third approach. You can actually trick spring to treat your path variable as a model attribute and then validate it.
1. Write a custom validator for your path variable
2. Construct a @ModelAttribute
for your path variable and then use @Validator
(yes not @Valid
as it doesn't let you specify a validator) on that model attribute.
@Component
public class NameValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return String.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
String name = (String) target;
if(!StringUtils.isValidName(name)) {
errors.reject("name.invalid.format");
}
}
}
@RequestMapping(value = "/path/{name}", method = RequestMethod.GET)
public List<Friend> getAllFriendsByName(@ModelAttribute("name") @Validated(NameValidator.class) String name) {
// your code
return friends;
}
@ModelAttribute("name")
private String nameAsModelAttribute(@PathVariable String name) {
return name;
}
这篇关于Spring REST 服务控制器未被@PathVariable 和@Valid 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!