Search code examples
springspring-bootspring-webfluxspring-web

Inject property value into annotation field


I'm developing my rest API and I would want to have a dynamic list of endpoints. I'm using following approach:

@Controller("/")
public class Resource {

    @PostMapping(path = {"request1", "request2"})
    public Mono<ResponseEntity> postData(ServerHttpRequest request) {
        return Mono.fromSupplier(() -> ResponseEntity.ok().build());
    }
}

So I would like to know if it's possible to retrieve values for path field of PostMapping dynamically from the properties?

Thanks in advance


Solution

  • Try this:

    @RestController
    public class Resource {
        @Value("${helloworld.endpoints}") String endpoints = "";
    
        @GetMapping(value = "/maybe/{wildcard}")
        public Mono<ResponseEntity> post(@PathVariable(value = "wildcard") String path) {
            boolean ok = Arrays.asList(endpoints.split(";")).contains(path);
            if (ok)
                return Mono.fromSupplier(() -> ResponseEntity.ok().build());
            else
                return Mono.fromSupplier(() -> ResponseEntity.notFound().build());
        }
    }
    

    application.properties:

    helloworld.endpoints=request1;request2;request3