简体   繁体   中英

How to get configuration value for static value in Quarkus

I am rewriting exceptions from an old system and everything was working, but I needed make BAD_REQUEST configurable.

private static final String BAD_REQUEST = "BDRQ";

I tried to just put ConfigProperty, but it doesn't work.

import javax.ws.rs.core.Response.Status;
import org.eclipse.microprofile.config.inject.ConfigProperty;

public class SXClientException extends RuntimeException {
  @ConfigProperty(name = "greeting.error", defaultValue = "BDRQ")
  public String BAD_REQUEST;

  private final RuntimeException runtimeException;

  public SXClientException(RuntimeException e) {
    super(e);

    this.runtimeException = e;
  }

  public Status getStatus() {
    if (BAD_REQUEST.equals(runtimeException.getMessage())) {
      return Status.BAD_REQUEST;
    }
    return Status.INTERNAL_SERVER_ERROR;
  }

  // ...
}

It probably doesn't work since I make them without any CDI.

catch (LegacyOMException e) {
    throw new SXClientException(e);
}

I would prefer to avoid creating another bean (and passing the value) just to compare one String. Any idea how can I read a configuration value for a static-ish value?

you can use org.eclipse.microprofile.config.ConfigProvider. Works for both static and non static members.

public static final String BAD_REQUEST = ConfigProvider.getConfig().getValue("greeting.error",String.class);

public final String BAD_REQUEST = ConfigProvider.getConfig().getValue("greeting.error",String.class);

use follow method:

Properties properties = new Properties();  
InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");  
properties.load(inputStream );  
System.out.println(properties.getProperty("a"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM