土:
私のアプリケーションの構造が似ています
私は以下のように注釈を作成しました: -
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SampleAnnotation {
}
次に、サンプルインターセプタを作成しました:
public class SampleInterceptor implements MethodInterceptor {
private static final Logger logger = LoggerFactory.getLogger(SampleInterceptor.class);
@Inject
SampleService sampleService; // this is not working
public Object invoke(MethodInvocation invocation) throws Throwable {
logger.info("SampleInterceptor : Interceptor Invoked");
Object result = invocation.proceed();
Observable<List<Sample>> observable = (Observable<List<Sample>>) result;
SampleSender sender = null;
List<Sample> sampleList = observable.toBlocking().first();
for(Sample sample : sampleList ) {
sender = new SampleSender();
sender.setBoolean(sample.isBoolean());
logger.info("Pushing Data into Sender");
sampleService.insert(String.join("_", "key", "value"), sender); // here getting NullPointerException because sampleService is null
}
return result;
}
}
その後、私は以下のようにGuiceModuleを作成しました: -
public class SampleModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), new SampleInterceptor());
}
}
私は上記の注釈を使用していているクラスがあります
// This class also have so many method and this was already declared and using in another services, I created a sample class here
class SampleClassForInterceptor {
// this sampleMethod() is not a new method, its already created,
// now I am adding annotation to it, because after finishing this functionality,
// I want something should be done, so created annotation and added here
@SampleAnnotation
public Observable<List<Sample>> sampleMethod() {
Sample sample = new Device();
sample.setName("*** 7777");
sample.setBoolean(true);
List<Sample> list = new ArrayList<>();
list.add(sample);
Observable<List<Device>> observable = Observable.just(list);
return observable;
}
}
私がしているRestModule
私が結合していた使用してSampleClassForInterceptor
、以下のように
public final class RestModule extends JerseyServletModule {
// other classes binding
bind(SampleClassForInterceptor.class).in(Scopes.SINGLETON);
// other classes binding
install(new SampleModule());
}
今、私は私が結合していているbootsrapクラスを持っています RestModule
public class Bootstrap extends ServerBootstrap {
binder.install(new RestModule());
}
使用法:-
@Path("service/sample")
public class SampleRS {
@Inject
SampleClassForInterceptor sampleClassForInterceptor;
public void someMethod() {
sampleClassForInterceptor.sampleMethod();
}
}
私の迎撃機能は、実行前に実行を開始しているsampleMethod()
のSampleClassForInterceptor
後、実行した後、クラスsampleMethod()
今ここに、インターセプタに再び来て背中を、私は(私たちはから得た結果を挿入するコードスニペットを持っていますsampleMethod()
)。これは私が取得していますところでNullPointerException
、私は、コードをチェックし、その見つかったSampleService
オブジェクトが注入され得ていないと、その値がありますnull
注:私は、RESTfulサービスのコンセプトでMicroservicesを使用しています
土:
私が使用したときrequestInjection
にSampleModule
、その後、SampleService
すなわち私が修正迎撃内部に注入しまっSampleModule
次のようにコードを
public class SampleModule extends AbstractModule {
@Override
protected void configure() {
SampleInterceptor interceptor = new SampleInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), interceptor);
}
}