This question already has answers here:
How to send NULL in HTTP query string?
(4 answers)
Closed 4 years ago.
What is the correct approach for sending null values in http post/get requests.
Important: I'm meaning to send NULL value for an specific param of the request, not empty strings or missing fields.
I considered the following options as incorrect
http://test.com?param1=¶m2=bar //this is an empty string, not a null
http://test.com?param1=null¶m2=bar //this is a string with content "null"
http://test.com?param2=bar //param 1 is missing, not null
Does sending a NULL make sense in HTTP (and is standardised) or should I fallback to any of the previous options? In the latter case, whats is the standard approach
In java when I use:
URLEncoder.encode(null, "UTF-8")
It crashes with Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
I also get a crash when using OkHttp3 lib and try to add a null param
FormBody.Builder bodyBuilder = new FormBody.Builder();
bodyBuilder.add("param1",null)
The only sensible option you did not disqualify in the question is parameter without value.
http://example.com?param1¶m2¶m3=foo
Though I would go for not adding the property at all to the query string, as it results in the value to be "not having a value" hich is what null usually means.
Just use a blank "" string instead of null param if you are add null param here it will show exception because it find a null value to send http request
Related
target = ObjectUtils.defaultIfNull(getCustomerByAddressID(sourceData, supportParam),
createNewCustomer(supportParam));
I am passing 2 functions to ObjectUtils.defaultIfNull. First is function which returns Customer if found by AddressID else null, second param is function which create new Customer.
After execution I am seeing 2 Customers, debug is showing even after getCustomerByAddressID(sourceData, supportParam) returns not null value - createNewCustomer(supportParam) is getting executed.
Is this issue because of Code formatting or what am I missing? Should I use Optional.ofNullable().orElse() instead of ObjectUtils.defaultIfNull?
You can use Optional.ofNullable(getCustomerByAddressID(sourceData, supportParam)).orElseGet(() -> createNewCustomer(supportParam)) if you don't want createNewCustomer(supportParam) to be invoked, even if getCustomerByAddressID(sourceData, supportParam) is null
You should use
target = Optional.ofNullable(getCustomerByAddressID(sourceData, supportParam))
.orElseGet(() -> createNewCustomer(supportParam));
Because both Optional.ofNullable().orElse() and ObjectUtils.defaultIfNull() which uses ternary operator internally, always call "orElse" part.
I'm a bit new to using Spring Expression Language but I'm trying to see what is the best way to check a that a Boolean value is not null and true?
For type "Display" there is a property "removeMessage" which is of type java.lang.Boolean.
So I want to first check that this value is not null and that it is true. Based on if this is true or not I will either return an empty string or a calculatedMessage.
I am currently checking with below expression:
display?.removeMessage != null && display?.removeMessage ? '' : display.calculatedMessage
While this works, I was hoping to find a more nicer way to do the null and true check. I was thinking/hoping for something more like how we would check normally such as e.g. BoolenUtils.isTrue(value) (apache), or even just checking like Boolean.TRUE.equals(value) which takes care of the null check.
Just having this should work , and already include your null check:
display?.removeMessage ? null : display.calculatedMessage
I am defining key values in application properties. It is reading all other values correctly but when I give a url as value it returns null. Any suggestion why.
I tried single quote, double quote and #"" but everytime it giving null for base url
a screen shot of application.properties file
server.port=8095
baseUrl=https://localhost:8080/api/gettingdata/singlevalue/employee
applicationId=APPLICATION_ID
userName=user_Name
password=pass_Word
directoryId=Directory_Id
in the above case it returns null for baseUrl value
Here's a service return, that gives us user's profile info:
{
email: 'someone#example.com',
pictureUrl: 'http://example.com/profile-pictures/somebody.png',
phone: null,
name: null
}
Now we get this JSON in our android app, and turn it into JSONObject model:
JSONObject profileInfo = new JSONObject(profileInfoJson);
And we bind UI views to data:
email.setText(profileInfo.getString("email"));
phone.setText(profileInfo.getString("phone"));
name.setText(profileInfo.getString("name"));
Then in our TextView or EditView we have null string, instead of having nothing.
It's possible that we check null values using if-then statements, but that's too much for a real-world application with so many fields.
Is there a way to configure JSONObject to gracefully handle null strings?
Update: I used optString with a fallback, as suggested, but it has no effect:
firstName.setText(profileInfo.optString("firstName", ""));
And the result is the same EditText has null in it.
Use optString, if no suitable value is found then the second parameter will be returned instead of exception or null
phone.setText(profileInfo.optString("phone","nophone"));
name.setText(profileInfo.optString("name","noname"));
Returns the value mapped by name if it exists, coercing(try to cast) it if
necessary, or fallback(return second parameter)if no such mapping exists.
I'm trying to create a string with a character length restriction of 12 for my JBOSS Seam project. The string must either be 12 characters or blank. My length annotation is correct which is the following:
#Length(min = 12,max = 12)
However when I try to put a null value in there I get an InvalidStateException: validation fail error. Any ideas how to allow this?
Null value for String and empty String are not the same thing. You are passing a null value (not a String of length 0). Check this out:
Difference between null and empty ("") Java String
Also, you should try out #Size(min=,max=).
Well I decided to not rely on the #Length annotation and instead created my own custom validator class to do the job and that worked out well. Thanks anyway!