【问题标题】:Moving from @Named to Custom Qualifier results in NPE从 @Named 移动到自定义限定符会导致 NPE
【发布时间】:2016-06-11 22:50:35
【问题描述】:

我有一个非常简单的 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 类:

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

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

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

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

使用@Named("message") 而不是@Message 可以正常工作,所以我错过了什么?

【问题讨论】:

    标签: jakarta-ee jax-rs cdi


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      相关资源
      最近更新 更多