У меня есть абстрактный класс в java ее приложении с внедренным HttpServletRequest:
abstract class MyRestClientAbstract {
@Inject
private HttpServletRequest request;
public void doStuff() {
/// do stuff with request...
}
, и я использую этот "doStuff" в вызовах функций, которые используются другими "Сервисами", которые не являются «Контроллеры» в весеннем понимании.
И когда я вызываю doStuff (), я получаю исключение:
org.jboss.weld.exceptions.IllegalStateException: WELD-000710: Cannot inject HttpServletRequest outside of a Servlet request
at org.jboss.weld.bean.builtin.ee.HttpServletRequestBean.newInstance(HttpServletRequestBean.java:53)
at org.jboss.weld.bean.builtin.ee.HttpServletRequestBean.newInstance(HttpServletRequestBean.java:40)
at org.jboss.weld.bean.builtin.AbstractDecorableBuiltInBean.create(AbstractDecorableBuiltInBean.java:52)
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:99)
at org.jboss.weldx.servlet.http.HttpServletRequest$$Proxy$_$$_WeldClientProxy.getHeader(Unknown Source)
at com.systematic.site.commons.restClients.RestClientAbstract.createRequestBuilderWithAuthHeader(RestClientAbstract.java:36)
at com.systematic.site.commons.restClients.RestClient.lambda$getListAsync$2(RestClient.java:75)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
at rx.observers.Subscribers$5.onNext(Subscribers.java:235)
at rx.internal.operators.OperatorDoAfterTerminate$1.onNext(OperatorDoAfterTerminate.java:50)
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.Subscriber.setProducer(Subscriber.java:205)
at rx.Subscriber.setProducer(Subscriber.java:205)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeUsing.call(OnSubscribeUsing.java:94)
at rx.internal.operators.OnSubscribeUsing.call(OnSubscribeUsing.java:32)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:45)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10150)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:45)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:30)
at rx.Observable.subscribe(Observable.java:10246)
at rx.Observable.subscribe(Observable.java:10213)
at rx.Observable.subscribe(Observable.java:10053)
Я пытался изменить @Inject на @Context, таким образом, я получил NPE, потому что запрос нулевой .
javaee-api: 7.0.jar используется.
PS Возможно, проблема в том, что Сервисы, использующие MyRestClientAbstract, являются @Singleton [s]. А когда ServletRequest пытались ввести, он не справился с одиночками?