Spring ConditionalOnProperty - understanding the "havingValue" property? - spring

The javadoc for org.springframework.boot.autoconfigure.condition.ConditionalOnProperty is like below -
public abstract java.lang.String havingValue
The string representation of the expected value for the properties.
If not specified, the property must not be equal to false.
Returns:
the expected value
Default:
"
What i don't understand in the above is If not specified, the property must not be equal to false.
If the property is not specified at all, then how can it be false (or true or xyz).
From the ConditionalOnProperty api doc - this part is clear.
If the property is not contained in the Environment at all, the matchIfMissing() attribute is consulted. By default missing attributes do not match.

Related

Exclude 0 from JSON response in Jackson Spring boot

I have a POJO like this.
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class Test {
private int a;
private String b;
}
I want to exclude the property 'a' if it has 0 value. String b is excluded with
#JsonInclude(JsonInclude.Include.NON_NULL)
Only way I could thing of is convert the int data type to Integer Object and set the value to NULL in the setter explicitly if it is 0.
Any other suggestions or correct solution will be appreciated
Option 1:
Do what you said: Change int to Integer and use #JsonInclude(Include.NON_NULL). Because primitive types have default values and their values cannot be compared to null you have to wrap the int to Integer. See Primitive Data Types. imho this is the cleaner way.
Option 2:
Use the way described in this answer and use #JsonInclude(Include.NON_DEFAULT) instead (see Jackson-annotations API), so that default values (and so also null values for objects) will be ignored.
Note:
If you only want to exclude the specific field (in your case the int/Integer - a - field) when it has a null-/default value and the other fields (in your case the String - b - field) should be included when they have null-/default values, put the annotation on field level.

How to define #Value as nullable in spring with kotlin?

I don't want autowiring fail when one specific property is missing. In that case i can accept nulls. How can i achieve it?
#Service
class MyClass(
#Value("\${my.nullable.property}") property: String?
)
You can define a default when using #Value and set it to an expression that evaluates to null:
#Value("\${my.nullable.property:#{null}}")
Anything after the : is the default if my.nullable.property cannot be defined. Because putting null there would be treated as a String, we have to use an expression that evaluates to null, which is wrapped by #{}.

Annotation with default values

In my code, I have several uses of the same annotation which has three property values, one of which always has the same value.
#ApiModelProperty(value = "some value",
allowableValues = "available,pending,sold", dataType="myclass.Payload")
dataType="myclass.Payload" is always the same value.
Is there anyway I can get a bit of code reuse here?

how to give default values in protocol buffer?

message Person {
required Empid = 1 [default = 100];
required string name = 2 [default = "Raju"];
optional string occupation = 3;
repeated string snippets = 4;
}
Can I give the default values as mentioned above?
For proto3, custom default values are disallowed.
Update: The below answer is for proto2 only, proto3 doesn't allow custom default values.
Yes, you can give default values as you had written. default is optional for required, but for optional you have to mention the default values else type specific value is automatically assigned. Moreover you forgot to mention the type for Empid.
protobuf language guide states that
If the default value is not specified for an optional element, a
type-specific default value is used instead: for strings, the default
value is the empty string. For bools, the default value is false. For
numeric types, the default value is zero. For enums, the default value
is the first value listed in the enum's type definition. This means
care must be taken when adding a value to the beginning of an enum
value list.

A property which is Calculated, SqlComputed, with a custom getter, all at the same time: what has the priority?

This is code extracted from this project (note: reformatted for clarity):
Class Util.Data.EmojiType Extends %Persistent
{
Property CodePoint As %Integer;
Property UnicodeChar As %String [
Calculated,
ReadOnly,
SqlComputeCode = { set {*} = $wchar({CodePoint})},
SqlComputed,
Transient
];
// snip
Method UnicodeCharGet() As %String
{
quit $wchar(..CodePoint)
}
Now, I really don't get it. Why is it that UnicodeChar is both Calculated and has a custom getter (ouch), plus the custom getter does exactly the same thing as the SqlComputeCode?
And if I try and get this property, what part of all this will be triggered?
Custom getter can be called even if property is not Calculated. But works only in object access mode. And to get calculated value via SQL query, property should have defined all properties: Calculated, SqlComputed and SqlComputeCode. And in case where SqlComputeCode is defined, this code uses only in SQL query. When property has Calculated property, but not SqlComputed, it will not appear in SQL result.
SqlComputed, Calculated

Resources