I have a json:
{
"clientId": "1",
"appName": "My Application",
"body": "Message body",
"title": "Title"
"data": {
"key1": "value1",
"key2": "value2"
}
}
And a DTO:
@Data
public class PushNotificationDto {
private Long clientId;
private String appName;
private String body;
private String title;
private String data;
}
I'm using SpringBoot and my @RestController looks like this:
@RestController
@AllArgsConstructor
public class PushNotificationController {
private PushNotificationService pushNotificationService;
@PostMapping("/push-notification")
void sendPushNotification(@RequestBody PushNotificationDto pushNotification) {
pushNotificationService.send(pushNotification);
}
}
Since data field in json object is actually an object but in my DTO it is a String, I get an exception:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Can not deserialize instance of java.lang.String out of START_OBJECT token;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of java.lang.String out of START_OBJECT token
What can I do so that such deserialization performs successfully?
In your request object you have an array of data
.
"data": {
"key1": "value1",
"key2": "value2"
}
But in your PushNotificationDto
object you have String data
. That's the reason you're getting this error. To solve this error you can change String data
to Map<String,String>