[英]Override HttpServletResponse sendError or setStatus
I'm working on a project where we are migrating code from Jetty to Jboss. 我正在一个项目中,我们正在将代码从Jetty迁移到Jboss。 This code is used as an API where different services is calling the service, the service perform the operation and returns the data, but if something goes wrong during the process exceptions are thrown and finally caught in the top level class.
此代码用作API,其中不同的服务在调用该服务,该服务执行该操作并返回数据,但是如果在处理过程中出错,则将引发异常,并最终将其捕获在顶级类中。
When using Jetty this worked good, hence the Jetty servlet engine added the message sent to the HttpServletResponse sendError function to the status line. 使用Jetty时,此方法效果很好,因此Jetty servlet引擎将发送到HttpServletResponse sendError函数的消息添加到了状态行。 But in Jboss, the default status is instead added:
但是在Jboss中,默认状态是添加:
Jetty: return: 500: My Custom error 码头:返回:500:我的自定义错误
Jboss: 500: Internal Server Error Jboss:500:内部服务器错误
I've not been able to add the *USE_CUSTOM_STATUS_MSG_IN_HEADER* to the Jboss, so I thought I might customize the response by creating a HttpServletResponseWrapper and overriding either the sendError or setStatus message to do what I need and return the content in a way I need it. 我无法将* USE_CUSTOM_STATUS_MSG_IN_HEADER *添加到Jboss中,所以我认为我可以通过创建HttpServletResponseWrapper并重写sendError或setStatus消息来自定义响应,以执行所需的操作并以所需的方式返回内容它。
But, I'm unsure on how to perform this, or if there are any better solutions. 但是,我不确定如何执行此操作,或者是否有更好的解决方案。
best, Henrik 最好的,亨里克
To wrap the HttpServletResponse, you'll want to create a Filter that creates the HttpServletResponseWrapper and passes it down the chain instead of the original response passed to doFilter(..)
: 要包装HttpServletResponse,您需要创建一个Filter ,该Filter创建HttpServletResponseWrapper并将其向下传递到链上,而不是传递给
doFilter(..)
的原始响应:
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
HttpServletResponseWrapper wrapper = new MyHttpServletResponseWrapper(resp);
chain.doFilter(req, wrapper);
}
private static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.