英文:
Not getting server response when passed as bean
问题
以下是您提供的内容的翻译部分:
我有一个控制器,在其中传递直接的Bean和@valid注解进行验证。我创建了一个自定义注解,并将其添加到bean中的每个字段。它进行了验证,但没有从自定义异常处理程序中提供消息。接下来,如果我不传递bean,而是为每个字段传递请求参数,那么它会从自定义异常处理程序中获得正确的响应。
您可以看到/group
在作为@RequestParam
传递时提供了验证的响应,而/check
不会提供响应消息,只会返回400的请求代码。
您可以看到在约束违规异常中传递的消息。我创建了一个仅验证特殊字符而不验证其他内容的自定义注解。对于RequestParam,它起作用。
英文:
I have a controller where I pass direct Bean and @valid annotation to validate. I have created a custom annotation and added it to each field in the bean. It validates but doesn't give messages from a custom exception handler. Next, if I don't pass bean and pass request param in for every field then it gives proper response from a custom exception handler.
You can see /group
gives the response of validation when passed as @RequestParam
and /check
doesn't give a response message just gives 400 request code.
I have added a response message in custom exception handler
You can see the message passed in constraints violation exception. I Have created custom annotation which just validates special characters nothing else. It works for RequestParam.
答案1
得分: 0
所以在这里,我解决了通过移除@Valid注解并添加了这段代码:
Set<ConstraintViolation<ProcessDefination>> violations = validator.validate(processDefination);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
英文:
So here I resolved the issue with removing the @Valid annotation and added this code
Set<ConstraintViolation<ProcessDefination>> violations = validator.validate(processDefination);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
答案2
得分: -1
如果响应中包含400
,可能问题不在于@Valid
注解,而是Group
被解释为了错误的参数。尝试删除此注解,看看是否仍然出现400
。
有一件事我不太明白,你只是使用@Valid
注解创建了一个GET请求,也许尝试同时添加@RequestParam
注解。在这个端点的上下文中,“Group” bean 到底是什么?
英文:
If 400
is in response maybe the problem is not in @Valid
annotation just Group
is interpreted as a bad parameter. Try deleting this annotation does 400 still appear?
One thing I don't understand is that you are creating a GET only with @Valid annotation maybe try adding also @RequestParam
to it. What exactly is Group bean in context of this endpoint?
[EDIT: added as answer because of low rep]
专注分享java语言的经验与见解,让所有开发者获益!
评论