We have a Spring binding which is converting strings to List
s, using default converters available with Spring.
For example if we have a, b, c
pushed from the form then the controller gets a List
with elements:
We don't have to do anything special in our code.
I have a problem dealing with commas in my data. If I submit a, x,z, b , c
here x,z
is actually a single String, but the Spring converter does not know that and assumes that it's a delimiter and makes up the List
like this:
Now I come to my questions:
,
(comma) in my data when submitting the form?I think there is actually no way to escape comma in spring properties that are converted to a list/array by spring (e.g. by means of a ConfigurationProperties
class or similar).
Maybe there is some obscure way using SpEL or something but I found it easier to specify list properties in the "index" notation (not sure how this is called correctly).
Your example translates like this
# results in ["a", "x", "z", "b", "c"]
some.property.list=a, x,z, b, c
# results in ["a", "x,z", "b", "c"]
some.property.list[0]=a
some.property.list[1]=x,z
some.property.list[2]=b
some.property.list[3]=c
The obvious drawback however is that you need to maintain the index manually. But usually you will only have a limited number of entries in such a configurable list (at least from my hitherto experience).
Another way would of course be switching to YAML configuration instead of properties:
some:
property:
list:
- "a"
- "x,z"
- "b"
- "c"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With