Using ConfigurationProperties to fill Map in generic way

前端 未结 4 1101
礼貌的吻别
礼貌的吻别 2020-12-13 08:49

I\'m wondering, if there is a generic way to fill a map with properties you just know the prefix.

Assuming there are a bunch of properties like

names         


        
4条回答
  •  感动是毒
    2020-12-13 09:13

    In addition to this, my problem was that I didn't had multiple simple key/value properties but whole objects:

    zuul:
      routes:
        query1:
          path: /api/apps/test1/query/**
          stripPrefix: false
          url: "https://test.url.com/query1"
        query2:
           path: /api/apps/test2/query/**
           stripPrefix: false
           url: "https://test.url.com/query2"
        index1:
           path: /api/apps/*/index/**
           stripPrefix: false
           url: "https://test.url.com/index"
    

    Following Jake's advice I tried to use a Map with a Pojo like this:

    @ConfigurationProperties("zuul")
    public class RouteConfig {
        private Map routes = new HashMap<>();
    
        public Map getRoutes() {
            return routes;
        }
    
        public static class Route {
            private String path;
            private boolean stripPrefix;
            String url;
    
            // [getters + setters]
        }
    }
    

    Works like a charm, Thanks!

提交回复
热议问题