I created an entity (Model or Data class) in a Spring-Boot (Kotlin) project which contains a field with the type Arraylist but when I send an array data in JSON format from Postman, the Array gets stored in the database as a long random string.
When I try to retrieve the data from the database I get the actual array, perfectly formated.
My question is why is an ArrayList stored in an H2 database like this???
Evaluation.kt
@Entity
data class Evaluation (
@Id val id : String,
val timeStamp : Long,
val symptoms : ArrayList<String>,
val travelHistory : Boolean,
val contactWithCovidPatient : Boolean,
val evaluatedBy : String,
var evaluationPercentage : String? = null,
@ManyToOne var user: User? = null
)
EvaluationController.kt
@RestController
class EvaluationController (val evaluationService: IEvaluationService) {
@PostMapping("evaluate/{userId}")
fun evaluateUser(@PathVariable userId : String, @RequestBody evaluation: Evaluation) : ResponseEntity<Evaluation> =
ResponseEntity.ok().body(evaluationService.addEvaluation(evaluation, userId))
}
Request Body JSON
{
"id":"e_01",
"timeStamp":"123456789",
"pinCode":"123457",
"travelHistory":true,
"contactWithCovidPatient":true,
"evaluatedBy":"u_01",
"symptoms": ["Fever","Cough"]
}
Response JSON
{
"id": "e_01",
"timeStamp": 123456789,
"symptoms": [
"Fever",
"Cough"
],
"travelHistory": true,
"contactWithCovidPatient": true,
"evaluatedBy": "u_01",
"evaluationPercentage": "95",
"user": {
"id": "u_01",
"name": "abc01",
"phoneNumber": "9876543210",
"pinCode": "123457",
"covidResult": "Positive"
}
}
H2 Database Table

This is a hex string representing the serialized ArrayList object. See Serializable Objects for details about object serialization in Java.
Running the following code yields the same result:
List<String> symptoms = new ArrayList<>(Arrays.asList("Fever", "Cough"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(symptoms);
byte[] serializedObject = byteArrayOutputStream.toByteArray();
String hex = Hex.encodeHexString(serializedObject); // Apache Commons Codec
System.out.println(hex);
aced0005737200136a6176612e7574696c2e41727261794c6973747881d21d99c7619d03000149000473697a657870000000027704000000027400054665766572740005436f75676878
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