Spring #Value escape colon(:) in default value - spring

I have the following property annotated with #Value. I have a default value defined using the default separator of ':"
#Value("${prop.url:http://myurl.com}")
Is there a way to escape the ':' in http://myurl.com or do I have to define a different separator value in my configuration.

Update:
For spring 4.2 and higher, no single quotes are needed. Spring will see the first colon as special, and use all the rest as a single string value.
For spring 4.2 and higher,
#Value("${prop.url:http://myurl.com}")
For the previous versions, I believe single quotes will do the trick:
#Value("${prop.url:'http://myurl.com'}")

If you need to pass a list of Strings that contain colon with default value then do like:
#Value("${parameterName:}#{T(java.util.Arrays).asList(\"abc:1\",\"def:2\")}")
private List<String> parameters;

On Spring version 3.2 the default value works without quotes.

Related

Jackson allow unquoted control chars disadvantages

Hi I use spring web and I need to send newline characters in my string by default I got 400 bad request.When I change allow-unquoted-control-chars parameter that time my request passed successfully
spring.jackson.parser.allow-unquoted-control-chars=true
in jackson release notes,I see this is non standard.My question is what is the correct way to pass unquoted chars like new line?If I change this parameter is there any security or another issue I can face?Is there any disadvantage to change this parameter
Since JSON specification requires quoting for all control characters, this is a non-standard feature, and as such disabled by default.

What is the syntax of Spring Boot "application.properties" file?

I've seen different delimeters for properties
: =
Can't find reference about it.
In application.properties you can specify properties in either of these forms:
Using =:
key=value
Using :
key: value
All examples in the Spring docs for application.properties use = as the delimiter. But Java properties also supports : so by extension Spring (which is utilising java.util.Properties under the hood also supports :. Here's the relevant extract from the java.util.Properties Javadoc ...
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator.

The correct format of application.properties in spring boot

I am confused here:
in the documentation on the properties section there is an example on how to modify stuff in
application.properties
spring.main.web_environment=false
spring.main.show_banner=false
Note that the above uses the "=" sign to apply values to the properties
In the spring-boot documentation in the logging section there is an example on how to modify logg levels
ogging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
Note that the above uses the ":" sign to apply values to the properties
What is the correct way to set properties? If you test it both ways works!
Is there a typo in the docs and the second example is actually for application.yaml?
From the javadoc of java.util.Properties:
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator.

Escaping a | (pipe) in SpEL

I'm hoping there is a very quick simple answer to this, I've read a few other questions on here that reference SpEL and escape sequences, but still haven't succeeded.
I would like to split a property into a list of String using #Value and SpEL. The property will be:
12345|12345|12345
So I need to split on pipe characters, I can do this in Java by using .split(\\|) to successfully escape the pipe character. I have tried with no slashes, 2 slashes and 4 slashes and all are unsuccessful. Is it possible to split on pipes using the following code? My client wants to keep using pipes for whatever reason...
#Value("#{'${list.of.blocked.people}'.split('\\|')}")
private List<String> myBlockedPeopleList;
Thanks.
Here is a link to the question that got me this far, for reference.
Reading a list from properties file and load with Spring annotation value
I also tried looking at the Spring Docs, but I couldn't find any reference to escape pipes in their documentatin.
Spring Documentation on SpEL
Found the answer on a web development forum here.
My solution was to basically give up on trying to escape the pipe character with back-slashes and use the unicode character escaped sequence. If anyone else is trying to pull in a pipe-separated String using #Value, the following code is tested and works with Java 6, Tomcat 6 and Spring 3.
// Reads blockPeopleString delimited with | and splits into List of Strings
#Value("#{'${blockPeopleString}'.split('\\u007c')}")
private List<String> blockPeopleList;

Freemarker escaping freemarker

I'm using freemarker to generate a freemarker template. But I need some way to escape freemarker tags.
How would I escape a <#list> tag or a ${expression} ?
You could also use: ${"$"}{expression} if you find the {} nesting confusing.
I'm using the alternative syntax feature. I start the template with [#ftl] and use this syntax.
For the expressions I use the string literal feature: ${r"${expression}"}
You can configure FreeMarker to use [=exp] instead of ${exp} (since 2.3.28), and [#...]/[#...] instead of <#...>|<#...> by setting both the interpolation_syntax and the tag_syntax configuration setting to square_bracket (in the Java API: Configuration cfg; ... cfg.setInterpolationSyntax(Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX) and cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX)). Then the syntax doesn't clash with the default syntax.
There's one tricky case; if the template starts with <#ftl>, then it will switch the tag syntax back to angle_bracket. To counter that, just add a [#ftl] line before it.
See also: https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.html
In the case when you want to use non-raw strings so that you can escape double quotes, apostrophes, etc, you can do the following:
Imagine that you want to use the string ${Hello}-"My friend's friend" inside of a string. You cannot do that with raw strings. What I have used that works is:
${"\x0024{Hello}-\"My friend's friend\""}
I have not escaped the apostrophe since I used double quotes.

Resources