4

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

Rajeev Ranjan
  • 3,588
  • 6
  • 28
  • 52

3 Answers3

1

Two way binding is not possible in Spring Properties.It can only read run time and environmental variables.

Please refer following link for more information.

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Configuration-Binding

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
  • Actually, the config is part of `application.properties` already. I need to set the placeholder value in a function. Will update the question perhaps its not clear. – Rajeev Ranjan Nov 15 '17 at 06:30
  • @RajeevRanjan - Edited my my answer based on your edit. – Pramod S. Nikam Nov 15 '17 at 06:48
  • Actually @Pra Jazz, the fetching part need not be done during jar deployment. It is some value that is built up within the function which is stored in a variable `str`. Then it is to be put in place of `{DOCID}`. – Rajeev Ranjan Nov 15 '17 at 08:06
1

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

Alisha Raju
  • 726
  • 6
  • 12
0

Try like this ...

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

And set run configuration as below

-DdocId = 133323 // any number which you want
myeongkil kim
  • 2,465
  • 4
  • 16
  • 22