Spring @Value escape colon(:) in default value
Asked Answered
E

3

59

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.

Entomologize answered 29/7, 2015 at 21:17 Comment(0)
B
92

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'}")
Blabber answered 29/7, 2015 at 21:23 Comment(1)
For me on Spring 4.2 that resulted in: 'http://myurl.com' (single quotes part of the value). Simply removing the single quotes fixed it. If I would guess, it splits on the first colon and the first part is the variable; the remainder is the value.Tashia
W
2

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;
Wersh answered 11/2, 2021 at 11:35 Comment(0)
F
0

On Spring version 3.2 the default value works without quotes.

Felixfeliza answered 18/2, 2019 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.