【发布时间】:2019-12-23 21:17:46
【问题描述】:
我有许多 Spring Boot 应用程序都引用了一个通用 jar。在通用代码中,我列出了我们遇到的异常。我需要查找在启动应用程序中捕获的这些异常并显示用户友好的错误消息。这些异常和错误消息需要在 .properties 文件中。最好的方法是什么?这不是 I18N - 只是从属性文件中查找。
这些应用程序的结构如下:
BootAppA
|__src/main/java
BootAppB
|__src/main/java
Common
|__src/main/java
我什至不确定 .properties 文件是否可以保存在公共 jar 中?同样,Spring Boot 配置并不存在 - 每个应用程序都有自己的配置。他们只是共享共同的罐子。
例如,如果发生 HTTP 502 错误,我希望在属性文件中有一个键值对,如下所示:
502 Bad Gateway = AWS service is temporarily not available. ESS has been notified.
我想查找键 502 Bad Gateway 并获取值 AWS service is tempoarily not available. ESS has been notified. 我不知道该怎么做。我在 SO 上看到的所有消息似乎都与 I18N 有关,并且涉及一个配置类,而我的普通 jar 没有。
我开始编写如下代码:
private static Map<String, String> errorsMap = new HashMap<>();
static {
errorsMap.put("502 Bad Gateway", "AWS service is tempoarily not available. ESS has been notified.");
}
但我需要将其外部化为属性文件。键是否必须是单个字符串,没有空格?也许我需要某种类来保存异常和用户友好的消息?但那我怎么在地图上查找呢?
【问题讨论】:
标签: spring spring-boot properties