I am running a spring boot project where I need to parse an erro code dynamically, but I have realised that if the key is of integer type, it return null. Is there a way to parse Integer Keys in YAML file
Parsing This property doesnt work
errorcode:
00001: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
Howver Parsing This property work
errorcode:
ONE: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
I am trying to read this property using
import org.springframework.core.env.Environment;
#Autowired
private Environment environment;
error= environment.getProperty("errorcode.ONE") Works
error= environment.getProperty("errorcode.00001") --Doesn't works
So in nutshell, if an integer has been used as a key, YAML file is returning value as null. One workaround is to use the key this way as shown below, but any better ideas?
error
code-001:
It can be solved by specifying that number as string in yaml. like,
errorcode:
'00001': An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
This worked for me.
Read all string from the yaml and replace the integer value with non integer value. Descerialize the class with non integer value as variable.
Related
I am trying to deploy a springboot application on kubernetese. I have stored all environment variable in config map and trying to read value from there. There are certain properties where we need to read int values.
In our code, it looks something like this :
application.properties :
TOKEN_RETRY_COUNT=3
As we have to read it from ConfigMap, we have updated our application.properties as mentioned below:
Update application.properties :
TOKEN_RETRY_COUNT=${STARGATE_TOKEN_RETRY_LIMIT}
Config Maps
The values are configured are like this in the Config Maps. I had to put double quotes as it wasn't allowed without any quote.
"STARGATE_TOKEN_RETRY_LIMIT": "3",
Now, when I am trying to read these values after deploying it on kubernetese, I am getting below error :
For STARGATE_TOKEN_RETRY_LIMIT :
Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "STARGATE_TOKEN_RETRY_LIMIT"
For now, I had updated code to take this value as String and then parse it as int but this is not ideal way. There should be some standard way to take different types.
If we can't handle different type in Config Maps, then what what would be ideal way to pass different application properties for Springboot application on kubernetese ?
I don't think the issue is related to the property value (3) being specified as a string ("3"). If you read the error message carefully you will see that the NumberFormatException is caused by an attempt to parse STARGATE_TOKEN_RETRY_LIMIT (literally) to a an int. Probably there is something wrong with the way the config map is passed to the application/container.
I am trying to add expression validation on my property on actionbean but I am unable to make it work. I have even tried with integers like this >0 but still the same exception is thrown. Below is the code
#Validate(required=true, minvalue=1, expression="${this > maxBudget}")
int minBudget;
int maxBudget;
I am getting the below exception:
net.sourceforge.stripes.exception.StripesRuntimeException: Could not parse the EL expression being used to validate
field minBudget. This is not a transient error. Please double check the following expression for errors: ${this > maxBudget}
caused by
javax.el.ELException: The identifier [this] is not a valid Java identifier as required by section 1.19 of the EL specification (Identifier ::= Java language identifier).
This check can be disabled by setting the system property org.apache.el.parser.SKIP_IDENTIFIER_CHECK to true.
I have tried few variation, but every time it throws this exception.
Can some one please point out the mistake I am doing here
thanks
If you want to make sure minBudget is larger than maxBudget (isn't that the other way around?) you could just do:
#Validate(required=true, minvalue=1, expression="${minBudget > maxBudget}")
For greater flexibility you could consider implementing a custom validation method:
#ValidationMethod(on={"show"})
public void checkBudgetRange(ValidationErrors errors) {
if (minBudget < maxBudget)
errors.addGlobalError( new SimpleError("This is not good..."));
// all sorts of other checks to your liking
}
The on array argument holds the name(s) of the event handler(s) for which you want to perform this validation method. So in the example here that would be public Resolution show().
There's an excellent explanation at the Stripes Framework site at https://stripesframework.atlassian.net/wiki/display/STRIPES/Validation+Reference
UPDATE:
If you want to make use of the this keyword in validation expressions you may need to add a VM argument to your server (tested this on Tomcat 8):
-Dorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
Otherwise the abovementioned error may be thrown.
The default value of org.apache.el.parser.SKIP_IDENTIFIER_CHECK was changed from true to false as of version 7 in Tomcat.
https://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html
https://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html
In the hibernate model when using jadira-usertype org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString as Type, the conversion is not reversible.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
private LocalDateTime requestedStartDate;
When creating object with datetime in following format;
myObject.setRequestedStartDate(LocalDateTime.parse("2014-12-28T19:30:00"));
is stored in DB (MariaDB) as "2014-12-28T19:30" ignoring the seconds part (don't know why).
when querying back the data, I'm getting the following exception
java.time.format.DateTimeParseException: Text '2014-12-28T19:30' could not be parsed at index 16
But, If I set date as "2014-12-28T19:30:01" with seconds set to "01", it is working fine.
I also tried setting the springframework's DateTimeFormat, still facing the same exception.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
#DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime requestedStartDate;
You can try this modified pattern "yyyy-MM-dd'T'HH:mm[:ss]".
The meaning of the square brackets is indicating an optional part during parsing - see javadoc.
I am writing a Grails (2.3.3 currently) application and have created a validateable command object similar to the following:
#Validateable
class MyCustomCommand {
String name
static constraints = {
name blank: false
}
}
In my i18n/messages.properties file I defined the following properties to override the default error messages.
MyCustomCommand.name.blank=Name must be provided.
MyCustomCommand.name.null=Name must be provided.
Which per the Grails documentation should be of the format [Class Name].[Property Name].[Constraint Code] as I have done. When I run my application if I leave the value blank I still get the default message for a null property.
I also tried following the example of the default messages and defining them a follows, but still get the default message.
MyCustomCommand.name.blank.message=Name must be provided.
MyCustomCommand.name.null.message=Name must be provided.
I am assuming that I am missing something simple here, but have yet to stumble upon what. Any suggestions on what I am doing incorrectly?
It is simple indeed. Message should look like:
myCustomCommand.name.blank=Name must be provided.
myCustomCommand.name.nullable=Name must be provided.
//className.propertyName.blank (camelCase with first letter of class name lower)
So, as I anticipated it was something simple. I was using the defaults as an example which used null where as what I really needed was nullable. Which does make sense as that matches the constraint name.
Therefore the correct version is:
myCustomCommand.name.blank=Name must be provided.
myCustomCommand.name.nullable=Name must be provided.
I downloaded the StackID api from the following link http://code.google.com/p/stackid/source/checkout
I followed the instructions in the readme file but am still encountering an exception.
The site crashes on this line in the Current.cs file
private static string SiteWideSalt { get { return KeyStore.GetKey(KeyStore.LatestKeyVersion).Salt; } }
The error I'm receiving is:
The type initializer for 'OpenIdProvider.Helpers.KeyStore' threw an exception.
{"Error converting value 1 to type 'OpenIdProvider.Helpers.KeyStore+Key'."} <--this is what's in the inner exception, which to me doesn't make sense since the property for value is byte and last I checked this should work with the number 1.
Any help would be appreciated, thanks!
I also had some issues there.
After putting the content of the key-gen file in square brackets "[]" to make it a JSON array I had to restart IIS and it works fine now.