简体   繁体   English

在 Springboot Java 中维护键值映射对的最佳方法

[英]Best way to maintain key-value mapping pairs in Springboot Java

I want to know the best way to handle key-value mapping in a Springboot API.我想知道在 Springboot API 中处理键值映射的最佳方法。

The request of the API is as: API的请求如下:

{
  "a": "1",
  "b": "b"
}

This API then calls a downstream API.此 API 然后调用下游 API。 But before it does that I need to convert the values of the fields a and b.但在此之前,我需要转换字段 a 和 b 的值。 The conversion would be as per:转换将按照:

For field a:
1 -> One,
2 -> Two,
3 -> TATA

for fields b:
b -> beta,
a -> alpha,
c -> c

Both of the above mappings are constant(wont change throughout) and hence need to be initialized and maintained in our API.上述两个映射都是恒定的(始终不会改变),因此需要在我们的 API 中进行初始化和维护。 Now once the mapping takes place the request to the downstream API will look like.现在一旦映射发生,对下游 API 的请求将如下所示。

{ 
  "a" : "One",
  "b" : "beta"
}

ie 1 is mapped to One for field a, and b is mapped to beta for field b;即对于字段a,1被映射到1,对于字段b,b被映射到beta; based on the key value mapping mentioned above.基于上面提到的键值映射。

Need to know the best practice in Java and Spring boot to achieve this.需要了解 Java 和 Spring boot 中的最佳实践来实现这一点。 How do I maintain the mapping table, initialize it just once and convert incoming API requests to corresponding values at runtime.我如何维护映射表,将它初始化一次并在运行时将传入的 API 请求转换为相应的值。

Can't use enum because some keys are numerical.不能使用枚举,因为有些键是数字的。 If Hash Map and Hast table are my best bets, then how do I initialize them and maintain them throughout, since the mapping is constant don't want to create them again and again for each API call?如果 Hash Map 和 Hast 表是我最好的选择,那么我如何初始化它们并在整个过程中维护它们,因为映射是恒定的,不想为每个 API 调用一次又一次地创建它们? Where do I read the key-value pairs from to initialize the hashmap/table?我从哪里读取键值对来初始化哈希图/表?

You could keep your translations in code in a private static final field, and initialize it in a static initialization block:您可以将您的翻译保存在私有静态 final 字段中的代码中,并在静态初始化块中对其进行初始化:

private static final Map<String, Map<String, String>> TRANSLATIONS_PER_KEY = new HashMap<>();

static {
    Map<String, String> translateForA = new HashMap<>();
    translateForA.put("1", "One");
    ...
    TRANSLATIONS_PER_KEY.put("a", translateForA);
    ...
}

Then you could do the translation using Guava Maps.transformEntries() :然后你可以使用Guava Maps.transformEntries()进行翻译:

    Map<String, String> translated =
            Maps.transformEntries(input, (key, value) ->
                    TRANSLATIONS_PER_KEY.get(key)
                            .getOrDefault(value));

If you want to avoid NullPointers, and return the original value if there is no translation, you could do it like this:如果你想避免 NullPointers,并在没有翻译的情况下返回原始值,你可以这样做:

    Map<String, String> translated =
            Maps.transformEntries(input, (key, value) ->
                    TRANSLATIONS_PER_KEY.getOrDefault(key, emptyMap())
                            .getOrDefault(value, value));

You may create enum for all mappings with pares您可以使用 pares 为所有映射创建枚举

@AllArgsConstructor
@Getter
public enum AType {
    ONE("One", "1"),
    TWO("Two", "2"),
    TATA("TATA", "3");

    private String translation;
    private String mapping;


    public String of(String v) {
        for (AType aType : values()) {
            if (aType.getMapping().equals(v)) {
                return translation;
            }
        }
        throw new IllegalArgumentException("try again");
    }
}

and new enum for bb 的新枚举

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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