gpt4 book ai didi

java - 带有请求消息的 Apache CXF ExceptionMapper

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:12 24 4
gpt4 key购买 nike

我想知道这样的事情是否可能,或者是否有更好的方法:

public class WebServiceExceptionMapper implements ExceptionMapper<Exception> {

private static Logger logger = LoggerFactory.getLogger(WebServiceExceptionMapper.class);

@Override
public Response toResponse(Exception ex, Message requestMessage) {
logger.error("request=" + httpMessage.etc..etc.., ex);
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}

}

基本上,我希望不仅能够记录异常,还能够记录触发错误的请求消息。更具体地说,如果请求包含 JSON 或 XML,我想获取它。

最佳答案

非 volatile 存储器。通过在我的 ExceptionMapper 中注入(inject) MessageContext 并添加自定义 RequestHandler 来解决这个问题,如下所述:http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-td4390185.html

public class StringContentRequestHandler implements RequestHandler { 

private static final Logger logger = LoggerFactory.getLogger(RawContentRequestHandler.class);

public Response handleRequest(Message m, ClassResourceInfo resourceClass) {
InputStream is = m.getContent(InputStream.class);
try {
CachedOutputStream bos = new CachedOutputStream();
IOUtils.copy(is, bos);

bos.flush();
is.close();

m.setContent(InputStream.class, bos.getInputStream());
StringBuilder builder = new StringBuilder();
bos.writeCacheTo(builder, "utf-8");
m.setContent(String.class, builder.toString());
return null;

} catch (IOException ex) {
logger.error("IOException on getting raw content", ex);
return null;
}
}
}

关于java - 带有请求消息的 Apache CXF ExceptionMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26006323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com