跟进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
注入构造函数的例子,而不是玻璃文件4.1附带的XmlRootElementJaxbProvider
子类中的最小例子 - 毕竟,我想重用它们的类层次结构,因为它是在链接的线程中完成的。
提前感谢任何建议。
答案 0 :(得分:2)
我无法解释异常的根本原因,但我遇到了同样的问题,当时并没有感觉像是钻研泽西源而已经比我已经拥有的更多了。
我最终懒得加载我的自定义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
}