Assign a value to a parameter in application properties before use

Viewed 12198

I have a property in my application.properties file in a SpringBoot project -

app.module.service.url=http://localhost:8090/{DOCID}

I have injected it into my Java file as below -

@Value("${app.module.service.url}")
private String url;

Now I need to replace DOCID with some value in my function(i.e-dynamic). How do I get to do this or am I completely missing it? I am aware that we can do this in case of message properties in Spring. But here I have nothing to do with Locales.

Edit:

There is some value which I need to put in place of {DOCID} when using it within my Java implementation.

...
public class Sample{

@Value("${app.module.service.url}")
private String url;

public void sampleFunc(){

String str = "random Value" //some dynamic value goes in here
...

Now I need to replace {DOCID} with str in url

3 Answers

You can use spring UriTemplate for this purpose.

In your implementation class:

...
public class Sample{

@Value("${app.module.service.url}")
private String url;

public void sampleFunc(){
 String dockId = someValue; // customize according to your need
 UriTemplate uriTemplate = new UriTemplate(url);
 URI uri = uriTemplate.expand(docId);
 new RestTemplate().postForEntity(uri, requestObhect, Responseclass.class);
}
...

For more information, you can refer : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriTemplate.html

Try like this ...

app.module.service.url=http://localhost:8090/{{docId}}

And set run configuration as below

-DdocId = 133323 // any number which you want
Related