Multiline values in application.yml for spring.cloud.function.definition - spring-boot

We have a Spring boot project using the deprecated #StreamListener and now we are switching to Spring Cloud Stream functional kafka binders.
The problem is that this service connects to multiple kafka topics and our single line spring.cloud.function.definition: topicA;topicB;topicC;...;topicN is becoming very long
I would like to know how to use on Spring's application.yaml the yaml capabilities such as multi line values (such as | or > operators) but I haven't found something similar on documentation.
My goal would be something such as
spring.cloud.function.definition: | topicA;
topicB;
topicC;
...;
topicN
Thanks

There are multiple ways to represent multiline values in YAML. Details could be found in How do I break a string in YAML over multiple lines? and most of them are supported in Spring and could be used in application.yml.
Using multiline for function definitions in Spring Cloud Stream
All above approaches works in Spring but at the end it really depends how the result value is used. In some cases new line character is preserved and in some cases replaced with space.
From what I see spring.cloud.function.definition is parsed in org.springframework.cloud.stream.function.FunctionConfiguration and logic doesn't expect any white spaces.
The only approach that would work in this case is double-quoted block with escaped the newline
spring.cloud.function.definition: "topicA;topicB;topicC;\
...;topicN"

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.

how to implement complex pattern matching in Spring batch using PatternMatchingCompositeLineMapper

How can we implement pattern matching in Spring Batch, I am using org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper
I got to know that I can only use ? or * here to create my pattern.
My requirement is like below:
I have a fixed length record file and in each record I have two fields at 35th and 36th position which gives record type
for example below "05" is record type which is at 35th and 36th position and total length of record is 400.
0000001131444444444444445589868444050MarketsABNAKKAAAAKKKA05568551456...........
I tried to write regular expression but it does not work, i got to know only two special character can be used which are * and ? .
In that case I can only write like this
??????????????????????????????????05?????????????..................
but it does not seem to be good solution.
Please suggest how can I write this solution, Thanks a lot for help in advance
The PatternMatchingCompositeLineMapper uses an instance of org.springframework.batch.support.PatternMatcher to do the matching. It's important to note that PatternMatcher does not use true regular expressions. It uses something closer to ant patterns (the code is actually lifted from AntPathMatcher in Spring Core).
That being said, you have three options:
Use a pattern like you are referring to (since there is no short hand way to specify the number of ? that should be checked like there is in regular expressions).
Create your own composite LineMapper implementation that uses regular expressions to do the mapping.
For the record, if you choose option 2, contributing it back would be appreciated!

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;

Resources