简体   繁体   中英

Read multiple properties file in one go using Spring Boot?

I went through the link: How to pass a Map<String, String> with application.properties and other related links multiple times, but still its not working.

I'm using Spring Boot and Spring REST example. Link Question: How to by default execute the latest version of endpoint in Spring Boot REST? .

I've created mapping something like this and simply read the mapping

get.customers={GET: '/app-data/customers', VERSION: 'v1'}
post.customers={POST: '/app-data/customers', VERSION: 'v1'}
get.customers.custId={GET: '/app-data/customers/{custId}', VERSION: 'v2'}

Code:

private String resolveLastVersion() {
   // read from configuration or something
    return "2";
}

Code:

@Component
@ConfigurationProperties
@PropertySource("classpath:restendpoint.properties")
public class PriorityProcessor {

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

    public Map<String, String> getPriority() {
        return priorityMap;
    }
}

Code:

I suggest the following implementation:

@ConfigurationProperties(prefix="request")
public class ConfigurationProps {
    private List<Mapping> mapping;

    public List<Mapping> getMapping() {
        return mapping;
    }

    public void setMapping(List<Mapping> mapping) {
        this.mapping = mapping;
    }
}

Class Mapping will denote the information about the single mapping:

public class Mapping {
    private String method;
    private String url;
    private String version;

    public Mapping(String method, String url, String version) {
        this.method = method;
        this.url = url;
        this.version = version;
    }

    public Mapping() {
    }

    // getters setters here
}

On the Configuration or spring boot application class (the one with main method): @EnableConfigurationProperties(ConfigurationProps.class)

In the properties file put:

request.mapping[0].method=get
request.mapping[0].url=/customers
request.mapping[0].version=1

request.mapping[1].method=post
request.mapping[1].url=/students
request.mapping[1].version=2

In Filter (I assume you followed my suggestion from the linked question):

    @Component
    @Order(1)
    public class LatestVersionFilter implements Filter {

       private List<Mapping> mappings;

       public LatestVersionFilter(ConfigurationProps props) {
          this.mappings = props.getMapping();
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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