Bean Validation 400 errors are returning default error page (html) instead of Response entity (json)

自作多情 提交于 2019-11-30 22:30:38

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".

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.

I went throw Jersey code and looks like it's the way Jersey works and IMO it's just a coincidence that it works fine on Tomcat. Jersey processes the validation (and probably not only validation) errors following way:

  1. org.glassfish.jersey.message.internal.CommittingOutputStream#flushBuffer(boolean)

    writes JSON error message to the Servlet OutputStream;

  2. org.glassfish.jersey.servlet.internal.ResponseWriter#commit()

    calls HttpServletResponse#sendError(int, String), that according to the Servlet spec:

    ... If data has been written to the response buffer, but not returned to the client (i.e. the response is not committed), the data in the response buffer must be cleared and replaced with the data set by these methods ...

    So Grizzly clears up buffer with the JSON error and replaces it with the default error page.

I'd suggest to file an issue @ Jersey issue tracker https://java.net/jira/browse/JERSEY

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!