Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Value from application.yml returns wrong value

In my application.yml file there is declared:

service:
  a: 
    b: 011600
    c: 011200

selecting value from .yml via @Value annotation:

@Value("${service.a.c}")
private String VALUE;

Instead of expected '011200' I am getting '4992', which isn't mentioned in .yml.

like image 309
alalambda Avatar asked Mar 16 '17 07:03

alalambda


1 Answers

You did not specify which version YAML your document has, and it looks like your parser defaults to version 1.1 (or version 1.1 compatibility). This causes values starting with 0 followed by digits to be interpreted as octal, and value of 011200 (octal) is 4736 (decimal), the value of 011600 (octal) is 4992.

In YAML 1.2, octal integer values should start with Oo and therefore 011200 will be the integer 11200

Try using:

%YAML 1.2
---
service:
  a:
    b: 011600
    c: 011200

and if that doesn't get you the value you want, you should consider using a compliant YAML parser (the YAML 1.2 specification is from 2009, so there has been ample time to get things right).

You can of course quote your integer scalars, but then they will be loaded as strings, not as integers. In that case you don't need to specify the version tag, both version 1.2 and 1.1 interpreted that the same way:

service:
  a:
    b: '011600'
    c: '011200'

If after getting the decimal values you specified in the file, you still get the wrong value for @Value("${service.a.c}") then, you should consider using a different access method (something that works if there is a dot in the key (e.g. serv.ice:) or, again, a different parser.

like image 100
Anthon Avatar answered Sep 29 '22 10:09

Anthon