如何在出现错误时在CXF SOAP OneWay请求中设置响应状态代码

时间:2018-01-30 07:30:53

标签: http cxf jax-ws

我已经使用Apache CXF(dropwizard应用程序)实现了@OneWay JAX-RS服务。当使用无效结构调用时,在DocLiteralInInterceptor中导致解组错误,http状态代码200将返回给客户端。为了使调用进程识别出错误,我需要返回状态400或500,以及Unmarshalling Error中的错误文本。

我认识到,在错误之后," in"解拦器链是解开的(拦截器handleFault方法以相反的顺序调用),所以我在" in" -chain(最后一次放卷)的开头安装了一个拦截器

Fault f = (Fault) e;
f.setStatusCode(Response.Status.BAD_REQUEST.getStatusCode());

在我的handleFault-Method中,我可以分离故障消息并识别unmarshall错误。但我没有成功地做出回应。

我试过

Response response = Response
    .status(Response.Status.BAD_REQUEST)
    .entity(Response.Status.BAD_REQUEST.getStatusCode() + " " + f.getLocalizedMessage())
    .build();
soapMessage.getExchange().put(Response.class, response);

message.put(Message.RESPONSE_CODE, Response.Status.BAD_REQUEST.getStatusCode()); 

    let conversation1 = Conversation()
    conversation1.subject = "1st conversation"
    conversationArray.append(conversation1)

响应集在哪里以及如何覆盖它?

任何建议的Tx。

1 个答案:

答案 0 :(得分:0)

我知道,这种情况已经晚了,但对于那些正在寻找解决方案的人来说:

在我的应用程序中,以下工作:

public void handleFault(SoapMessage soapMessage) {
    /* some code to test for specific error deleted */
    Exchange exchange = soapMessage.getExchange();
    Message outMessage = exchange.getOutMessage();
    if (outMessage == null) {
        Endpoint endpoint = exchange.get(Endpoint.class);
        outMessage = endpoint.getBinding().createMessage();
        exchange.setOutMessage(outMessage);
    }
    try {
        EndpointReferenceType target = exchange.get(EndpointReferenceType.class);
        Conduit conduit = exchange.getDestination().getBackChannel(soapMessage);
        exchange.setConduit(conduit);
        conduit.prepare(outMessage);

    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
    }
    Object resp = outMessage.get("HTTP.RESPONSE");

    if (resp != null && resp instanceof HttpServletResponse) {
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setStatus(Response.Status.BAD_REQUEST.getStatusCode());
    }
    soapMessage.getInterceptorChain().abort();
}