本文介绍了Bean验证400错误返回默认错误页面(html)而不是响应实体(json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JUnit测试套件:GrizzlyHttpServerFactory + Jersey + Bean验证(jersey-container-grizzly2-servlet/jersey-bean-validation版本2.12,grizzly-http-server版本2.3.16,hibernate-validator ver 5.0.0.Final)

I have a JUnit testsuite: GrizzlyHttpServerFactory + Jersey + Bean Validation(jersey-container-grizzly2-servlet/jersey-bean-validation ver 2.12, grizzly-http-server ver 2.3.16,hibernate-validator ver 5.0.0.Final)

由ValidationException生成的400个错误正在返回Grizzly的默认错误页面(html)而不是Bean验证的Response实体(json).我已经尝试过ClientResponseFilter及其EntityStream还包含html错误页面.

The 400 errors generated by a ValidationException are returning Grizzly's default error page (html)instead of the Bean Validation's Response entity (json). I've tried a ClientResponseFilter and itsentityStream also contains the html error page.

当我在Tomcat下运行系统时,ValidationExceptions返回带有json格式的响应实体.

When I run the system under Tomcat, the ValidationExceptions return a Response with a json-formattedentity.

关于如何将Grizzly/Jersey/Validator配置为不返回错误页面的任何想法(html)并像Tomcat一样将ValidationExceptions放入Response的entityStream中?

Any ideas on how to configure Grizzly/Jersey/Validator to NOT return the error page (html)and put the ValidationExceptions into the Response's entityStream, just like Tomcat?

迈克·诺曼

推荐答案

查看alexey针对泽西岛2.13指向的代码,我发现代码路径通过将属性jersey.config.server.response.setStatusOverSendError设置为"true"可以避免出现问题.

After looking into the code to which alexey pointed to for Jersey 2.13, I found out that the code path in question can be avoided by setting the property jersey.config.server.response.setStatusOverSendError to "true".

因此,作为一种解决方法,直到 JERSEY-2673 固定,我才将其放置property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");进入我的ResourceConfig类,并能够在浏览器中看到自定义错误响应.

So, as a workaround until JERSEY-2673 is fixed, I just placed property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"); into my ResourceConfig class and was able to see the custom error responses in the browser.

这篇关于Bean验证400错误返回默认错误页面(html)而不是响应实体(json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:35