Я закончил тем, что использовал файл yaml, подобный этому:
shipping_options:
aramex: "Aramex"
aramex_cash_on_delivery: "Aramex COD"
australia_post: "Australia Post"
boxberry_delivery: "Boxberry Delivery"
boxberry_locker: "Boxberry Pickup and Postamats"
Подпрограмма вот так:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ShippingOptions {
@JsonProperty("shipping_options")
Map<String, String> shippingOptions;
}
И код для получения ключа:
public static String getShippingOptionValue(String key) throws IOException {
String baseDir = System.getProperty("user.dir");
String filePath = baseDir + "/src/test/resources/shippingoptions.yaml";
File yaml = new File(filePath);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
ShippingOptions shippingOptions = null;
try {
shippingOptions = mapper.readValue(yaml, ShippingOptions.class);
} catch (FileNotFoundException e) {
log.info("file could not be found {}", filePath);
} catch (IOException e) {
log.info("could not load shipping options from {}", filePath);
throw new IOException("shipping options YAML file was unable to be converted by Jackson {}", e);
}
assert shippingOptions != null;
String value = shippingOptions.getShippingOptions().get(key);
log.info("Shipping option retrieved was {}", value);
}
Надеюсь, это кому-нибудь поможет.