使用Jersey中的配置属性 [英] Working with configuration properties in Jersey

查看:147
本文介绍了使用Jersey中的配置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java / jetty自托管服务器和jersey-2 for java RESTful api。
应用程序包含带有属性的application.properties文件。

I use java/jetty self-hosted server and jersey-2 for java RESTful api. Application has application.properties file with properties.

ConfigurationProperties 类读取属性文件并将其加载到 java.util.Properties class。

ConfigurationProperties class reads and loads properties file into java.util.Properties class.

Jetty服务器实例化按以下方式完成。

Jetty server instantiation is done in the following way.

     // Create and register resources
    final ResourceConfig resourceConfig = new ApiServiceConfig()
            .register(new DependencyInjectionBinder());

    ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);

    contextHandler.setContextPath("/mydomain/api");
    Server jettyServer = new Server(8585);
    jettyServer.setHandler(contextHandler);

    ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(resourceConfig));
    contextHandler.addServlet(jerseyServlet, "/*");

    // Create web context. Can't use.
    //WebApplicationContext webContext = getWebApplicationContext();
    // Add web context to servlet event listener.
    //contextHandler.addEventListener(new ContextLoaderListener(webContext));

    try {
        jettyServer.start();
        jettyServer.join();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        jettyServer.destroy();
    }

我不能使用Spring AnnotationConfigWebApplicationContext 因为它需要commons-logging依赖,这在java-8中不起作用。

I can't use Spring AnnotationConfigWebApplicationContext as it requires commons-logging dependency which doesn't work in java-8.

如何使用jetty / jersey上下文注册属性以及如何在以后检索值(例如: context.getProperty(prop.name) ))?

How can I register Properties with jetty/jersey context and how can I retrieve values later(eg.: context.getProperty("prop.name"))?

推荐答案

你可以......



只需将属性对象配置为注射剂,并将其注入您需要的任何地方

You could...

Just configure the Properties object as an injectable, and inject it into wherever you need it

final Properties props ...
resourceConfig.register(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(props).to(Properties.class);
    }
});

@Path("config")
public class ConfigResource {

    @Inject
    private Properties properties;

}



你可以......



使用 InjectionResolver 和自定义注释使单个属性可注入

You could...

Make the individual properties injectable, using an InjectionResolver and a custom annotation

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public static @interface Config {
    String value();
}

public class ConfigInjectionResolver implements InjectionResolver<Config> {

    private final Properties properties;
    public ConfigurationInjectionResolver(Properties properties) {
        this.properties = properties;
    }

    @Override
    public Object resolve(Injectee injectee, ServiceHandle<?> handle) {
        if (String.class == injectee.getRequiredType()) {
            Config annotation = injectee.getParent().getAnnotation(Config.class);
            if (annotation != null) {
                String prop = annotation.value();
                return properties.getProperty(prop);
            }
        }
        return null;
    }
    ...
}

final Properties props...
resourceConfig.register(new AbstractBinder(){
    @Override
    protected void configure() {
        bind(new ConfigInjectResolver(props))
                .to(new TypeLiteral<InjectionResolver<Config>>(){});
    }
});

然后只需将它与自定义注释一起使用

Then just use it with the custom annotation

@Path("config")
public class ConfigResource {

    @Config(PROP_KEY)
    private String propValue;

    @GET
    public String getConfigProp() {
        return propValue;
    }
}



你可以......



使用小型图书馆我做了

<dependency>
    <groupId>com.github.psamsotha</groupId>
    <artifactId>jersey-properties</artifactId>
    <version>0.1.1</version>
<dependency>

resourceConfig.register(JerseyPropertiesFeature.class);
resourceConfig.property(JerseyPropertiesFeature.RESOURCE_PATH, "appication.properties");

@Path("test")
public class SomeResource {

    @Prop("some.prop")
    private String someFieldProp;

    private String someConstructorProp;

    public SomeResource(@Prop("some.prop") String someConstructorProp) {
        this.someConstructorProp = someConstructorProp;
    }

    @GET
    public String get(@Prop("some.prop") String someParamProp) {
        return someParamProp;
    }
}



你可以......



使用Spring。我认为使用Java 8时遇到的问题是你使用的是Spring 3.x.我认为Java 8不受支持。我使用带有java 8的Jersey / Spring4没有问题。如果您使用的是jersey-spring3依赖项,则需要排除spring3依赖项,并添加spring 4.请参阅此帖子中的更新

另请参阅:

  • Configuration Properties with Jersey

这篇关于使用Jersey中的配置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆