Put value of application.properties in annotation @GetMapping

1k views Asked by At

I'm tried to call a get http response with spring boot, and I have a value of url in application.properties, I'm tried to call this value in annotation @GetMapping like that :

@GetMapping("${service.url}")

But Spring boot says "Cannot resolve @PathVariable service.url"

Maybe there are another way to get this value ?

Thanks for your help everyone.

2

There are 2 answers

0
nulldroid On

In your Controller class, you can pass an object of type org.springframework.core.env.Environment in the constructor, e.g. named env.

Then you can just call

@GetMapping("${env.getRequiredProperty("service.url")}")
2
susheem_k On

Try using SpEL. In your case, this can be modelled as something like

@GetMapping("#{'${service.url}'}")