I am developing a Spring Boot application where I use AWS SDK for fetching data from DynamoDB. I am running into the problem where DynamoDB Type converter is attempting to parse a Double
as an Integer
, resulting in a NumberFormatException
.
My entity class is as follows:
import com.amazonaws.services.dynamodbv2.datamodeling.*;
import lombok.*;
import org.springframework.data.annotation.Id;
import java.time.LocalDate;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "my_table")
public class MyEntity {
@Id
@Getter(value = AccessLevel.NONE)
private MyKey myKey;
// Other attributes...
@DynamoDBAttribute(attributeName = "overall_energy")
@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.N)
private Double overallEnergy;
// Other attributes...
}
The data I am trying to fetch from DynamoDB looks like this:
{
"overall_energy": {
"N": "29985.689834184945"
}
}
However, when I attempt to fetch this data, I get a NumberFormatException
:
Jun 9 22:20:39 ip-10-55-167-190 web: at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Jun 9 22:20:39 ip-10-55-167-190 web: Caused by: java.lang.NumberFormatException: For input string: "29985.689834184945"
Jun 9 22:20:39 ip-10-55-167-190 web: at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) ~[na:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at java.base/java.lang.Integer.parseInt(Integer.java:668) ~[na:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at java.base/java.lang.Integer.valueOf(Integer.java:999) ~[na:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters$ToInteger$2.convert(StandardTypeConverters.java:773) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters$ToInteger$2.convert(StandardTypeConverters.java:770) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters$1.unconvert(StandardTypeConverters.java:75) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$DelegateConverter.unconvert(DynamoDBTypeConverter.java:109) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$NullSafeConverter.unconvert(DynamoDBTypeConverter.java:128) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$ExtendedConverter.unconvert(DynamoDBTypeConverter.java:88) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.unconvert(DynamoDBMapperFieldModel.java:146) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.unconvertAndSet(DynamoDBMapperFieldModel.java:164) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
Jun 9 22:20:39 ip-10-55-167-190 web: at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:267) ~[aws-java-sdk-dynamodb-1.12.454.jar!/:na]
It seems the SDK is attempting to parse the Double
as an Integer
, but I don't understand why this is happening. I am using version 1.12.454 of the AWS SDK.
All references to overallEnergy
in my codebase treat it as a Double
.
I tried the following steps to solve it:
-
Created a Custom Converter (StringToDoubleTypeConverter
)
Expectation: I expected that by explicitly converting the string from the database to a double in Java using my own converter, it would resolve the issue.
Result: This approach did not work. The NumberFormatException
was still being thrown.
-
Removed Explicit Type Conversion Annotations
Expectation: I thought perhaps the issue was with the explicit type conversion annotations, so I removed them to let the DynamoDBMapper handle the conversion implicitly.
Result: The issue was not resolved. The NumberFormatException
persisted.
-
Modified the overall_energy
Value in DynamoDB Record
Expectation: Suspecting that the issue might be related to the size of the number or the precision, I changed the value of overall_energy
in the DynamoDB record to only have two decimal places.
Result: This did not resolve the issue. The NumberFormatException
was still being thrown.
-
Changed overall_energy
to an Integer Value
Expectation: I changed the value of overall_energy
in the DynamoDB record to an integer value by removing the decimal values. This was a test to see if the issue was solely related to handling decimal values.
Result: This change worked in the sense that the NumberFormatException
was not thrown and the number was parsed correctly. However, the value was treated as an integer and not a double, which does not meet the needs of my application. The application requires this field to be treated as a double
value.
Does anyone know why this is happening and how I could resolve this? Any help would be greatly appreciated.
Thank you in advance.
Hi vtjean,
Thanks for your suggestion, greatly appreciated!
It's good to know that the explicit conversion annotation is not required. However, part of my troubleshoots before posting this question, also included removing the annotation, see #2 of my troubleshoot step. This did however not solve the problem.
I would like to hear if you have any other suggestions.