我正在从旧系统中重写异常,并且一切正常,但是我需要使BAD_REQUEST
可配置。
private static final String BAD_REQUEST = "BDRQ";
我试图只放置ConfigProperty,但是它不起作用。
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;
}
// ...
}
自从我没有任何CDI的情况下,它可能就不起作用了。
catch (LegacyOMException e) {
throw new SXClientException(e);
}
我宁愿避免只为了比较一个String而创建另一个bean(并传递值)。知道如何读取静态值的配置值吗?
答案 0 :(得分:2)
您可以使用 org.eclipse.microprofile.config.ConfigProvider。 适用于静态和非静态成员。
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);
答案 1 :(得分:0)
使用关注方法:
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");
properties.load(inputStream );
System.out.println(properties.getProperty("a"));