将异常从CXF拦截器传播到异常映射器

我有一个流程,在CXF客户端上我安装了拦截器,提供程序和异常映射程序。就我而言,我正在通过拦截器捕获来自客户端的不良响应,然后我想中止cxf总线链并引发故障。不幸的是我做不到,因为在每种情况下都只记录从拦截器抛出的异常,但是主要错误(错误的json格式)会传播到异常映射器。我想避免使用异常映射器,但是我不知道怎么做。我正在使用WebClient来实现这样的拦截器:

@Component

public class MyInterceptor extends AbstractPhaseInterceptor<Message> {

public MyInterceptor() {

super(POST_STREAM);

}

@Override

public void handleMessage(Message message) throws Fault {

if (message != null) {

//message.getExchange().setOneWay(true);

//message.getExchange().put(Exception.class, new MyException());

//message.getInterceptorChain().abort();

//message.setContent(Exception.class, new MyException());

//Endpoint ep = message.getExchange().get(Endpoint.class);

//message.getInterceptorChain().abort();

//if (ep.getInFaultObserver() != null) {

// ep.getInFaultObserver().onMessage(message);

//}

//throw new WebApplicationException( new MyException());

//message.setContent(Response.class, response);

throw new Fault(new MyException());

}

}

我读到我应该实现jaxrs-filter,因为拦截器引发的异常不会传播到异常映射器。多亏了WebClient的实现,在Java中有没有办法做到这一点?

S client = create(url, clazz, list(jsonProvider(), providers));

WebClient.getConfig(client).getInInterceptors().add(new MyInterceptor());

我也尝试过在拦截器上使用不同的阶段,但是也没有用。

回答:

我一直在研究和测试您的问题。问题是从CXF拦截器抛出的异常逃避了JAX-

RS流(请参阅

CXF团队的答案)

Fault从拦截器产生可以被获取,实现handleFault在拦截器本身

 public void handleFault(Message message) {

Exception e = message.getContent(Exception.class);

}

或实施FaultListener并在CXF总线上注册

WebClient.getConfig(client).getBus().getProperties().put("org.apache.cxf.logging.FaultListener",new MyFaultListener());

public class MyFaultListener implements FaultListener{

public boolean faultOccurred(final Exception exception,final String description,final Message message) {

//return false to avoid warning of default CXF logging interceptor

return false;

}

}

我发现实现所需行为的解决方法是将Response替换为可以由您的常规方法调用处理的自定义对象

进入Interceptor.handleMessage检查你所需要的条件,并创建一个Response自定义状态和实体。之后,停止链条

public class MyInterceptor extends AbstractPhaseInterceptor<Message> {

public MyInterceptor() {

super(Phase.POST_STREAM);

}

@Override

public void handleMessage(Message message) throws Fault {

if (message != null) {

//check the condition to raise the error

//build the custom Response replacing service call

Response response = Response

.status(Response.Status.BAD_REQUEST)

.entity("custom error")

.build();

message.getExchange().put(Response.class, response);

//abort interceptor chain in you want to stop processing or throw a Fault (catched by handleFault)

//message.getInterceptorChain().abort();

//throw new Fault (new MyException());

}

public void handleFault(Message messageParam) {

}

}

创建JAXRS客户端时,将ResponseExceptionMapper添加为提供者

providers.add(new ResponseExceptionMapper<WebApplicationException>() {

@Override

public WebApplicationException fromResponse(Response r) {

return new WebApplicationException(r);

}

});

YourService proxy = JAXRSClientFactory.create(url, clazz,providers);

Client client = WebClient.client(proxy);

WebClient.getConfig(client).getInInterceptors().add(new MyInterceptor());

此后,对的调用proxy.yourService()将引发WebApplicationExceptionif如果完成拦截器检查。您可以按期望的方式抓住它或将其扔掉

try{

proxy.yourService();

}catch (WebApplicationException e){

}

希望这可以帮助

以上是 将异常从CXF拦截器传播到异常映射器 的全部内容, 来源链接: utcz.com/qa/398937.html

回到顶部