0

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

Alex
  • 1,940
  • 2
  • 18
  • 36
  • Annotation variables must be compile-time constants, so you cannot inject any variable in there. Why not create a wildcard endpoint a-la ``@PostMapping(value = "/{id}")`` and use @PathVariable? Then you can filter out the endpoints which are not in your application.properties. – maslick Nov 28 '18 at 18:39
  • Directly in the controller? It will also work but I thought there was a possible solution like I've proposed – Alex Nov 28 '18 at 18:44
  • Yes. You can make use of the ``@Value`` annotation, e.g.: ``@Value("${helloworld.endpoint}") String endpoint;`` – maslick Nov 28 '18 at 18:46
  • Actually, it won't solve my problem because in the annotation field I can add only constants – Alex Nov 28 '18 at 19:01
  • see my answer below. hope it helps – maslick Nov 28 '18 at 19:32

1 Answers1

0

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
maslick
  • 2,903
  • 3
  • 28
  • 50