我们在CXF中实现了一个REST API.我的目标是能够在POJO上定义自定义注释并在它们获得编组之前在CXF拦截器中处理它们.我相信除了检索拦截器中的实际对象之外,我还需要能够做到这一点的所有信息.我的代码看起来像这样:
资源类
@Path("/mypath")
public class MyResource {
@GET
public MyObject getObject() {
MyObject o = new MyObject();
...
return o;
}
}
Run Code Online (Sandbox Code Playgroud)为MyObject
public class MyObject {
private String x;
@MyAnnotation
public String getX() {
return x;
}
public String setX(x) {
this.x = x;
}
}
Run Code Online (Sandbox Code Playgroud)拦截器
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public VersionOutInterceptor() {
super(Phase.POST_LOGICAL);
}
public final void handleMessage(Message message) {
// 1. STUCK -- get object from the message
// 2. parse annotations and …Run Code Online (Sandbox Code Playgroud)我在类中有一个方法,我想拦截:
[CustomTag1(Order = 0)]
[CustomTag2(Order = 1)]
public virtual DoSomething()
Run Code Online (Sandbox Code Playgroud)
ICallHandler.Order在使用时,如何将订单值注入属性CustomAttributeMatchingRule?
我不希望将订单硬编码到处理程序本身或注册时.我希望它是方法注释的Order属性的变量.
两个方法在同一个EJB中使用2x相同的Interceptor:
...
@Interceptors(PerformanceAuditor.class)
public Date refreshIfNecessary() {
// there is also the PerformanceAuditor-Interceptor on this method
Pair<Date,String> lastImportDate = importDbDAO.findLatestImportLog();
someContainer.reloadIfNecessary(lastImportDate);
return lastImportDate.getLeft();
}
@Interceptors(PerformanceAuditor.class)
public boolean checkAndRefreshIfNecessary(final Date importDate) {
Date lastImportDate = refreshIfNecessary();
return lastImportDate.after(importDate);
}
...
Run Code Online (Sandbox Code Playgroud)
现在我们使用以下结果在外部调用此EJB方法:
那么这里发生了什么?
加载实体后是否有任何事件发生?我的目标是将此实体添加到 Solr 或 Lucene 索引。EmptyInterceptor OnLoad 事件在对象初始化之前发生。我想在设置对象属性后处理事件。是否可以使用 Hibernate Interceptors 或其他东西?
我有一个流程,在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());
}
}
Run Code Online (Sandbox Code Playgroud)
我读到我应该实现jaxrs-filter,因为拦截器抛出的异常不会传播到异常映射器。由于WebClient的实现,在Java中有什么方法可以做到吗?
S client = create(url, clazz, list(jsonProvider(), providers));
WebClient.getConfig(client).getInInterceptors().add(new MyInterceptor());
Run Code Online (Sandbox Code Playgroud)
我也尝试过在拦截器上使用不同的阶段,但是也没有用。
所以我写了这个小应用程序,在其中显示了从Wikipedia获取的一些信息。此提取的HTML内也有链接。
所以我想做的是:
每次用户单击链接时,我都想拦截该链接并执行自定义行为,而不是默认的浏览器重定向。
Angular httpinterceptor中的构建在这里不起作用。我如何获得这种效果?
编辑:似乎我误解了href和http请求的工作方式。我仍然想对应用程序上单击的每个链接进行自定义行为。有没有办法拦截那些“事件”?
编辑:对于Angular 2+解决方案,请看下面我的答案。它是一种拦截标签的服务。
我想在服务器端为多个方法使用相同的拦截器,因为它们都具有相同的请求类型(StateRequest):
rpc apply (StateRequest) returns (StateResponse) {}
rpc cancel (StateRequest) returns (StateResponse) {}
rpc remove (StateRequest) returns (StateResponse) {}
rpc development (StateRequest) returns (StateResponse) {}
rpc implement (StateRequest) returns (StateResponse) {}
rpc draft (StateRequest) returns (StateResponse) {}
Run Code Online (Sandbox Code Playgroud)
拦截器:
public class ServerUserRoleAuthInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
String callName = call.getMethodDescriptor().getFullMethodName();
logger.info("Call: {}", callName);
// TODO: validate request (`StateRequest` instance)
return next.startCall(call, headers);
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我在React项目中使用axios进行API调用,我想在axios拦截器的api调用的请求和响应之间全局添加加载或旋转效果,这是我的拦截器的代码。
import Axios from 'axios'
Axios.interceptors.request.use(function (config) {
// spinning start to show
const token = window.localStorage.token;
if (token) {
config.headers.Authorization = `token ${token}`
}
return config
}, function (error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function (response) {
// spinning hide
return response;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud) 想象一下,我有以下代码:
const object = {};
// an error should be thrown
object.property.someMethod();
// an error should be thrown
object.foo;
Run Code Online (Sandbox Code Playgroud)
在someMethod()调用或调用任何其他不存在的属性时是否可能抛出错误?
我想我需要用它的原型做一些事情,抛出一个错误.但是,我不确定我应该做什么.
任何帮助,将不胜感激.
我找不到关于如何在 NestJS 中测试拦截器的任何解释
这个简单的示例拦截了一个 POST 查询,以将一个属性添加到正文中提供的示例模型中。
@Injectable()
export class SubscriberInterceptor implements NestInterceptor {
async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<ExampleModel>> {
let body: ExampleModel = context.switchToHttp().getRequest().body;
body = {
...body,
addedAttribute: 'example',
};
context.switchToHttp().getRequest().body = body;
return next.handle();
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试拦截函数中发生了什么。
迄今为止:
const interceptor = new SubscriberInterceptor();
describe('SubscriberInterceptor', () => {
it('should be defined', () => {
expect(interceptor).toBeDefined();
});
describe('#intercept', () => {
it('should add the addedAttribute to the body', async () => {
expect(await interceptor.intercept(arg1, arg2)).toBe({ ...bodyMock, addedAttribute: …Run Code Online (Sandbox Code Playgroud) interceptor ×10
java ×4
cxf ×2
javascript ×2
angular ×1
aop ×1
axios ×1
c# ×1
ejb ×1
grpc ×1
hibernate ×1
href ×1
httprequest ×1
java-ee ×1
jax-rs ×1
jestjs ×1
lucene ×1
message ×1
nestjs ×1
object ×1
pojo ×1
reactjs ×1
rest ×1
rpc ×1
solr ×1
typescript ×1
unit-testing ×1
web-services ×1