简体   繁体   English

Apache CXF:如何从拦截器返回失败响应

[英]Apache CXF: how to return failure response from an interceptor

I have written a custom interceptor that does some parameter validation. 我编写了一个自定义拦截器来进行一些参数验证。 I want to be able to return an error code and serialize a JAXB-annotated class as the response body. 我希望能够返回错误代码并将带有JAXB注释的类序列化为响应主体。

If I throw a WebApplicationException, it doesn't have any special processing done to serialize the Response object inside (which makes sense; I assume that is done by another interceptor). 如果我抛出一个WebApplicationException,它没有任何特殊的处理来序列化里面的Response对象(这是有道理的;我认为这是由另一个拦截器完成的)。

How should I go about stopping the interceptor chain but still have JAXB serialize the response entity? 我应该如何停止拦截器链但仍然有JAXB序列化响应实体?

Well, at least in the CXF JAX-RS interceptor flow, if you set: 好吧,至少在CXF JAX-RS拦截器流程中,如果你设置:

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

...then the actual service does not get invoked, while the other phases do get invoked. ...然后实际服务不会被调用,而其他阶段会被调用。 Haven't dug in to the CXF code to see where that logic kicks in. 没有深入研究CXF代码,看看这个逻辑在哪里发挥作用。

So I built a response like this: 所以我建立了这样的响应:

Response response = Response
    .status(Response.Status.FORBIDDEN)
    .entity(new ErrorEntity("This is a JAXB object with an error string"))
    .build();

I also have some custom authentication running in a CXF JAX-RS filter and I only want to check the parameters when the authentication is alright, so I set my parameter interceptor class to run during the PRE_INVOKE phase. 我还在CXF JAX-RS过滤器中运行了一些自定义身份验证,我只想在身份验证正常时检查参数,因此我将参数拦截器类设置为在PRE_INVOKE阶段运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM