不在作为 bean 传递时收到服务器响应。

huangapple 未分类评论51阅读模式
英文:

Not getting server response when passed as bean

问题

以下是您提供的内容的翻译部分:

我有一个控制器,在其中传递直接的Bean和@valid注解进行验证。我创建了一个自定义注解,并将其添加到bean中的每个字段。它进行了验证,但没有从自定义异常处理程序中提供消息。接下来,如果我不传递bean,而是为每个字段传递请求参数,那么它会从自定义异常处理程序中获得正确的响应。

添加截图。不在作为 bean 传递时收到服务器响应。

您可以看到/group在作为@RequestParam传递时提供了验证的响应,而/check不会提供响应消息,只会返回400的请求代码。

我在自定义异常处理程序中添加了响应消息。不在作为 bean 传递时收到服务器响应。

您可以看到在约束违规异常中传递的消息。我创建了一个仅验证特殊字符而不验证其他内容的自定义注解。对于RequestParam,它起作用。

不在作为 bean 传递时收到服务器响应。

英文:

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.

Adding screenshots.不在作为 bean 传递时收到服务器响应。

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不在作为 bean 传递时收到服务器响应。

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.

不在作为 bean 传递时收到服务器响应。

答案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&lt;ConstraintViolation&lt;ProcessDefination&gt;&gt; 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]

huangapple
  • 本文由 发表于 2020年5月29日 17:32:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/62082829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定