【问题标题】:Jersey constructor injection with @Context not working带有@Context的泽西构造函数注入不起作用
【发布时间】:2016-06-07 15:41:20
【问题描述】:

跟进this SO thread,我的问题是我是否在这里做一些根本错误的事情:

使用 GlassFish 4.1,我正在尝试编写具有如下构造函数的 MessageBodyReader:

package com.acme;

import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.Providers;
import javax.ws.rs.Consumes;

@Provider
@Consumes( "application/xml" )
public class MyMessageBodyReader implements MessageBodyReader<Object> {
   public MyMessageBodyReader(@Context Providers ps) {
      // try to do something with ps here
      // e.g. pass to a superclass constructor,
      // as required by the solution in the other thread
   }

   [...] // implemented interface methods
}

但无论我在配置方面做什么,我总是会得到一系列这些异常:

2016-06-07T17:19:28.484+0200|WARN: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 1
org.jboss.weld.exceptions.CreationException: WELD-001530: Cannot produce an instance of class com.acme.MyMessageBodyReader.
    at org.jboss.weld.injection.producer.NonProducibleInjectionTarget.produce(NonProducibleInjectionTarget.java:55)
    at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory$2.getInstance(CdiComponentProvider.java:242)
    at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory.provide(CdiComponentProvider.java:189)
    [...]

紧随其后

WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
org.jboss.weld.exceptions.CreationException: WELD-001530: Cannot produce an instance of class com.acme.MyMessageBodyReader.
    at org.jboss.weld.injection.producer.NonProducibleInjectionTarget.produce(NonProducibleInjectionTarget.java:55)
    at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory$2.getInstance(CdiComponentProvider.java:242)
    at org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider$CdiFactory.provide(CdiComponentProvider.java:189)
    [...]
MultiException stack 2 of 2
java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:389)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:461)
    at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:114)
    [...]

我发现了几个将Providers 注入构造函数的示例,尤其是在 GlassFish 4.1 附带的 XmlRootElementJaxbProvider 的子类中 - 毕竟,无论如何我都想重用它们的类层次结构,因为它在链接的线程。

提前感谢您的任何建议。

【问题讨论】:

  • 这是否仍在使用您在其他线程中描述的修改后的 MessageBodyFactory?如果您在 Jersey 中破解了核心类并对其进行了重建,并且您收到表明无法配置该类的错误 - 这两件事可能是相关的。另外,您的代码现在具有零可移植性...
  • 注意,我不是其他线程的作者。我还没有像 OP 那样修改 MessageBodyFactory,因为我想先看看我是否可以启动并运行自定义的 MessageBodyReader/Writer。

标签: java jersey glassfish jax-rs glassfish-4


【解决方案1】:

我无法解释异常的根本原因,但我遇到了同样的问题,当时我不想再深入研究 Jersey 的源代码。

我最终懒洋洋地加载了我的自定义 MessageBodyReader/Writers,因此我可以将 Providers 注入到包装器中。它不漂亮,但很有效。

类似的东西:

@Provider
@Consumes( "application/xml" )
public class MyLazyMessageBodyReader implements MessageBodyReader<Object> {
  @Context
  private Providers ps;

  private MessageBodyReader<Object> reader;

  @Override
  public final Object readFrom(
          Class<Object> type,
          Type type1,
          Annotation[] antns,
          MediaType mt,
          MultivaluedMap<String, String> mm,
          InputStream in)
          throws IOException, WebApplicationException
  {
    return this.getLazyReader().readFrom(type, type1, antns, mt, mm, in);
  }

  private MessageBodyReader<Object> getLazyReader() {
    if (this.reader == null) {
      this.reader = new MyMessageBodyReader(this.ps);
    }
    return this.reader;
  }
}

public class MyMessageBodyReader implements MessageBodyReader<Object> {
   public MyMessageBodyReader(Providers ps) {
      // try to do something with ps here
      // e.g. pass to a superclass constructor,
      // as required by the solution in the other thread
   }

   [...] // implemented interface methods
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    相关资源
    最近更新 更多