将属性值注入注释字段

时间:2018-11-28 17:52:37

标签: spring spring-boot spring-webflux spring-web

我正在开发我的其余API,并且希望有一个动态的端点列表。我正在使用以下方法:

@Controller("/")
public class Resource {

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

所以我想知道是否可以从属性中动态检索PostMapping的路径字段的值吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

@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