1

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:

PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

3

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();
       }
    }
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • BTW, from what you're trying to achieve - consider using stuff like Spring Cloud Gateway project, Zuul 2 or even Nginx. If you'll have multiple microservices that have a common entry point (a gateway) - maybe you can do everything there.... – Mark Bramnik Feb 10 '20 at 08:35
  • I agree, Thanks for the great solution. This is best way to implement it if not using Microservices architecture. However I'm stuck with the small issue posted here: https://stackoverflow.com/questions/60146766/unable-to-read-properties-in-map-using-spring-boot – PAA Feb 10 '20 at 08:43