Reading empty string from consul using spring-cloud-consul - java

I'm migrating from file based .properties file to consul based configuration in my spring application. I'm using spring-cloud-consul. Earlier in my property file I had a property like following
test.key=
In spring application class corresponding field is like this
#Value("${test.key:defaultVal}")
private String testConsul;
In the runtime, the value of testConsul string is an empty string. But when using the consul, whenever I put key test.key without a value, in the runtime it gets resolved to a null.
Is there anyway I can pass an empty string value through consul ?

This is the work around we're using. use a default value which is not exist
${no.such.key:}
Hope this helps.

Related

Can you override a library's Spring property placeholders?

We are working on moving our application to use only Spring-Boot application.properties files. The old way we were doing was that each library/dependency would have their properties stored in a dedicated properties file like res/environment/some-library-override.properties. The values would then be retrieved in the library using #Value("$some-library-{PROPERTY_NAME}").
However, since moving all of these override properties to dedicated application.properties files, it is no longer resolving the properties and we get errors like java.lang.NumberFormatException: For input string: "$some-library-{PROPERTY_NAME}".
I assume this is because it is still expecting the property to be in that dedicated properties file.
Is there a solution to this that doesn't involve modifying the library/dependency? Is it possible to have it ignore the prefix and only look for the PROPERTY_NAME in the application.properties files?
if you have declared propertie var likeproperty.name=XXXX or added environment var like PROPERTY_NAME=XXXX.
you need to use this way
#Value("some-library-${property.name}")
// will inject value "some-library" + "XXXX"

Spring Expression Language condtional application properties

I'm trying to get a value from the application properties file with condtional. If property a doesn't exits take property b.
But I can't get it to work. I have read about "SPEL" but cannot really understand it. I have tried this
#Value("'${api.path}' ?: '${api.backuppath}'")
private String path;
This doesnt work because it needs value on both property to work. In my case the first property can be null or the second can be null.

Spring SpEL. How to set null as default value for field

I am trying to set variiable throw SpEL in spring boot application:
#Value("${data.jndi-name:#{null}}")
private String jndiDataSource;
data.jndi-name should comes from application-{profile}.properties. But field jndiDataSource always null even if data.jndi-name exists.
Code with #Value("${data.jndi-name}") work fine but jndiDataSource contains empty string.
My question is how to set null to variable if property does not exists using SpEL.
upd: values comes from profile specific property file
try this:
#Value("${data.jndi-name:#null}")
private String jndiDataSource;

Java bean gets property name instead of value

I have a .properties file with bunch of properties in it. Here's an example:
mes.mail.debug=true
cookie.sso.domain = .stuffStuff.com
blabla.endpoint = blabla.com
test.value.property = myValue
The problem is with the last one (Which I have just added to the project we're working on).
I read the properties using #Value("${PropertyName}") annotation and it was working perfectly until lately, when I use the same thing, the variable gets the propertyName instead of its value:
#Value("${test.value.property}")
private String mProperty;
so, mProperty gets "test.value.property" where what I'm looking for is for it to get "myValue".
What's happening exactly? Is there something wrong with my project? I have tested in my friend's computer and it works perfectly.
By the way, i'm using Spring Tool Suite.
EDIT: It turns out that it doesn't detect the changes I make in the properties file. So if I change an old property's value; it acts as if nothing happened.
Does anyone has any idea why it's doing like this?
When you declare a Property Placeholder Configurer to load the properties files, you can set it to ignore unresolvable placeholders.
This means that if the property you are injecting with #Value is not found, its name (or key) will be assigned to the variable.
In your case, this option is enabled and the file that has been loaded by the application is not the one you are editing.
To see from where the file is been loaded, check the placeholder configurer location property.

SpringBoot SpringApplicationBuilder

I need to test my Spring application code by creating a Cucumber integration test. I am using SpringApplicationBuilder to start up my application before the actual logic is triggered and am using the following syntax to do so:-
application = new SpringApplicationBuilder()
.parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
.profiles("abc")
.properties("name:value") [It has 5/6 (name:value) pairs here]*
.showBanner(false)
.logStartupInfo(true)
.headless(true)
.application()
.run();
My Spring application starts up correctly. However, it does not get the values for the property (name, value) pairs that I am passing to the SpringApplicationBuilder(). I have tried the following to set them :-
Using name value pairs as above
List item using a HashMap of (name, value) pairs
Creating a ConfigurableEnvironment, retrieving the MutablePropertySources
and setting my properties in it.
None of these options are working, so when the application starts up and the code tries to access certain System Property values, it breaks.
Any ideas how this could be fixed.. All the help is greatly appreciated!
I need these properties to be Spring properties to make sure the app works correctly. Maybe I can test my code using the Spring props in some other way? If so, how do I do it?
You can configure the properties as below:
application = new SpringApplicationBuilder()
.parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
.profiles("abc")
.properties("key1:test1", "key2:test2")
.showBanner(false)
.logStartupInfo(true)
.headless(true)
.application()
.run();
Now, retrieve the properties using #Value annotation:
#Value("${key1}")
String val;
The val variable will be assigned the value test1
I've dealt with the same issue myself.
My problem was that the keys whose values I was trying to set were also set in an application.properties file.
After removing the entries from the file, it worked.

Categories