eclipse-microprofiles 配置抛出没有找到 ConfigProviderResolver 实现

eclipse-microprofiles config throws No ConfigProviderResolver implementation found

我在我的球衣 2.26 中使用 eclipse-microprofiles-config v1.1。

我的完整build.gradle如下

apply plugin: 'java'
apply plugin: 'war'

repositories {
    mavenCentral()
}

war{
    archiveName = 'pqr.war'
}

dependencies {
    compile 'javax:javaee-api:7.0'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile 'org.glassfish.jersey.core:jersey-common:2.26'
    compile 'org.glassfish.jersey.core:jersey-client:2.26-b03'
    compile 'org.glassfish.jersey.core:jersey-server:2.26'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.26'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
    compile 'org.glassfish.jersey.media:jersey-media-moxy:2.26'
    compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
    compile 'org.eclipse.microprofile.config:microprofile-config-api:1.1'
}

我有位于 {PROJECT_ROOT}/src/main/webapp/META-INF/microprofile-config.properties 的所需属性文件 其中的内容是

// Higher ordinal value so that this configuration source will take precedense
config_ordinal = 599999999 
foo.bar = abcdefghijklmnopqrstuvwxyz

每当我尝试将此配置设置为

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

@Path(value = "awesomeService")
public class AwesomeService {

    @GET
    @Path(value = "test")
    public String someOperation() {

        Config config = ConfigProvider.getConfig(); // fails here
        String value = config.getValue("foo.bar", String.class);

        return value;
    }

}

它抛出 "No ConfigProviderResolver implementation found" 尽管 ConfigProviderResolver.class 在类路径中。我使用 glassfish 4 进行部署。我哪里弄错了。以下是来自 glassfish 的服务器日志。

[2017-12-09T20:34:17.296+0530] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=27 _ThreadName=http-listener-1(1)] [timeMillis: 1512831857296] [levelV$
lue: 900] [[
  StandardWrapperValve[org.pqr.rest.AppConfig]: Servlet.service() for servlet org.pqr.rest.AppConfig threw exception
java.lang.IllegalStateException: No ConfigProviderResolver implementation found!
        at org.eclipse.microprofile.config.spi.ConfigProviderResolver.instance(ConfigProviderResolver.java:122)
        at org.eclipse.microprofile.config.ConfigProvider.<clinit>(ConfigProvider.java:74)
        at org.pqr.rest.AwesomeService.someOperation(AwesomeService.java:16)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:144)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)

可以在此处下载导致此问题的示例项目。 https://github.com/kalpacg/eclipse-microprofile-config

错误正确。 MicroProfile Config 是一个API,一个规范。它不是一个实现。要实现,您可以使用 Geronimo Config 之类的东西作为独立库。

另请注意,您可能需要将属性文件放在构建中的 src/main/resources/META-INF 下,以防 属性 加载失败。