简体   繁体   English

重写HttpServletResponse sendError或setStatus

[英]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.

相关问题 JBoss如何实现HttpServletResponse setStatus()与sendError() - How does JBoss implement HttpServletResponse setStatus() vs sendError() Mockito:捕获HttpServletResponse#sendError() - Mockito: capturing HttpServletResponse#sendError() HttpServletResponse#sendError()是否会抛出IOException吗? - Does HttpServletResponse#sendError() ever throw an IOException? setstatus(500)和senderror(500)之间有什么区别 - what's the difference between setstatus(500) and senderror(500) 在HttpServletResponse.sendError之后的过滤器中需要返回 - Return Needed in Filter After HttpServletResponse.sendError HttpServletResponse.sendError()不会重定向到错误页面 - HttpServletResponse.sendError() does not redirect to error page 使用HttpServletResponse.setStatus后如何获取状态 - How to get the status after using HttpServletResponse.setStatus 如何将消息从 HttpServletResponse.sendError(status, msg) 传递给控制器​​? - How to pass the msg from HttpServletResponse.sendError(status, msg) to controller? HttpServletResponse .sendError 在缺少 CSRF 令牌时返回空响应正文 - HttpServletResponse .sendError returns empty response body when missing CSRF token 我如何从HttpServletResponse.sendError检索味精 - how can i retrieve the msg from HttpServletResponse.sendError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM