如何从Dropwizard资源中的config.yml文件获取字符串? [英] How to get String from config.yml file in Dropwizard resource?

查看:117
本文介绍了如何从Dropwizard资源中的config.yml文件获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Dropwizard config.yml 中获取String并从资源类访问它。

I want to geta String in my Dropwizard config.yml and access it from a resource class.

我已将该类添加到配置中

I have added the class to the configuration

public class DropwizardBackendConfiguration extends Configuration {

  @NotEmpty
  private String uploadFileLocation;

  @JsonProperty
  public String getUploadFileLocation() {
    return uploadFileLocation;
  }

  @JsonProperty
  public void setUploadFileLocation(String uploadFileLocation) {
    this.uploadFileLocation = uploadFileLocation;
  }

}

我能够获取内容在运行方法

I am able to get the content in the run method

 public void run(
      final DropwizardBackendConfiguration configuration, final Environment environment) {
...
 System.out.println(configuration.getUploadFileLocation());


}

但是我如何在我的资源类。

But how can I get this value in my resource class.

推荐答案

如果要使用完整的 DropwizardBackendConfiguration 或只是Jersey资源中的 uploadFileLocation ,则必须将其作为构造函数参数传递。

If you want to use the complete DropwizardBackendConfiguration or just the uploadFileLocation in a Jersey Resource, you will have to pass it as a constructor argument.

入门指南用<$进行说明c $ c> HelloWorldResource 。在此示例中,有两个构造函数参数:

The Getting Started guide illustrates this with the HelloWorldResource. In this example there are two constructor arguments:

public HelloWorldResource(String template, String defaultName)

此类的实例为 run 方法中注册的

An instance of this class is registered in the run method:

@Override
public void run(HelloWorldConfiguration configuration,
                Environment environment) {
    final HelloWorldResource resource = new HelloWorldResource(
        configuration.getTemplate(),
        configuration.getDefaultName()
    );
    environment.jersey().register(resource);
}

使用您的配置和资源类执行类似的操作。

Do something similar using your configuration and your resource class.

这篇关于如何从Dropwizard资源中的config.yml文件获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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