@Value annotation type casting to Integer from Str

2019-03-17 02:33发布

I'm trying to cast the output of a value to an integer:

@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;

The above throws the error

org.springframework.beans.TypeMismatchException: 
    Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; 
nested exception is java.lang.NumberFormatException: 
    For input string: "(java.lang.Integer)${api.orders.pingFrequency}"

I've also tried @Value("(java.lang.Integer)${api.orders.pingFrequency}")

Google doesn't appear to say much on the subject. I'd like to always be dealing with an integer instead of having to parse this value everywhere it's used.

Workaround

I realize a workaround may be to use a setter method to run the conversion for me, but if Spring can do it I'd rather learn something about Spring.

9条回答
欢心
2楼-- · 2019-03-17 03:10

This problem also occurs when you have 2 resources with the same file name; say "configurations.properties" within 2 different jar or directory path configured within the classpath. For example:

You have your "configurations.properties" in your process or web application (jar, war or ear). But another dependency (jar) have the same file "configurations.properties" in the same path. Then I suppose that Spring have no idea (@_@?) where to get the property and just sends the property name declared within the @Value annotation.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-17 03:12

I was looking for the answer on internet and I found the following

@Value("#{new java.text.SimpleDateFormat('${aDateFormat}').parse('${aDateStr}')}")
Date myDate;

So in your case you could try with this

@Value("#{new Integer.parseInt('${api.orders.pingFrequency}')}")
private Integer pingFrequency;
查看更多
来,给爷笑一个
4楼-- · 2019-03-17 03:13

If you are using @Configuation then instantiate below static bean. If not static @Configutation is instantiated very early and and the BeanPostProcessors responsible for resolving annotations like @Value, @Autowired etc, cannot act on it. Refer here

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
查看更多
聊天终结者
5楼-- · 2019-03-17 03:15

In my case, the problem was that my POST request was sent to the same url as GET (with get parameters using "?..=..") and that parameters had the same name as form parameters. Probably Spring is merging them into an array and parsing was throwing error.

查看更多
Juvenile、少年°
6楼-- · 2019-03-17 03:16

Assuming you have a properties file on your classpath that contains

api.orders.pingFrequency=4

I tried inside a @Controller

@Controller
public class MyController {     
    @Value("${api.orders.pingFrequency}")
    private Integer pingFrequency;
    ...
}

With my servlet context containing :

<context:property-placeholder location="classpath:myprops.properties" />

It worked perfectly.

So either your property is not an integer type, you don't have the property placeholder configured correctly, or you are using the wrong property key.

I tried running with an invalid property value, 4123;. The exception I got is

java.lang.NumberFormatException: For input string: "4123;"

which makes me think the value of your property is

api.orders.pingFrequency=(java.lang.Integer)${api.orders.pingFrequency}
查看更多
手持菜刀,她持情操
7楼-- · 2019-03-17 03:17

I had the same issue I solved using this. Refer this Spring MVC: @Value annotation to get int value defined in *.properties file

@Value(#{propertyfileId.propertyName})

works

查看更多
登录 后发表回答