从 @Named 移动到自定义限定符导致 NPE

Moving from @Named to Custom Qualifier results in NPE

我有一个非常简单的 JAX-RS 服务:

@Path("/test")
public class Service {

    @Inject
    @Message
    String thingie;

    @GET
    public String test(){
        return thingie;
    }
}

字符串来自 @Produces 注释方法的地方:

public class Producer {
    @Produces
    @Message
    public String thingie(){
        return "thingie";
    }
}

@Message 限定符如下所示:

@Qualifier
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Message {
}

为了完整起见,我拥有的另一个代码是 JAX-RS Activator class:

@ApplicationPath("/rest")
public class JAXRSActivator extends Application {
}

还有一个空 beans.xml。我使用的应用服务器是 Wildfly 10.0.10.

当我对 /rest/test 进行 GET 时,我得到了 500 的响应,内容类型为 plain/text,文本为 "java.lang.NullPointerException"。但日志或消息中没有更多信息(特别是 NPE 来自的位置)。

我的生产者方法被调用,但我在服务中的 test() 方法 class 没有被调用。

使用@Named("message") 代替@Message 效果很好,所以我错过了什么?

tldr;将@Message 放入一个包中。

因此,正如您可能猜到的那样,这是一个用来测试某些东西的玩具示例,我将其全部放入默认包中。

Hibernate 验证器不处理默认包中的注释,因为它假设会有一个包。