Deserializing webhook events in java
-
Hello -
I'm writing a service to receive the webhook events from FusionAuth. I'd like to deserialize the json into a pojo. It looks like this was provided in 2019:https://github.com/lob/lob-java/pull/153
The solution looks pretty straight-forward:
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); Event event = mapper.readValue(newEvent, Event.class);
I can't tell from the above which Event type is being used. If it's somewhere in the com.log.model hierarchy, which library is that in? Is there a Maven dependency for it?
Thank you -
Brian.
-
@brian_ls Ok - After searching around a bit I added io.fusionauth/fusionauth-java-client (1.27.2) to my dependencies and changed the snippet to:
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
BaseEvent event = mapper.readValue(newEvent, BaseEvent.class);Where BaseEvent is io.fusionauth.domain.event.BaseEvent.
Unfortunately, the second line above throws an exception:com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class io.fusionaut
h.domain.event.BaseEvent]: missing type id property ’type’
at [Source: (String)"{"event":{"type":"user.registration.create","applicationId":"b5d6a4a3-79ac-4413-a76b-f4432f6ad12d"The text of the event is below, which does include a 'type' field... Any help is appreciated:
{
"event": {
"type": "user.registration.create",
"applicationId": "b5d6a4a3-79ac-4413-a76b-f4432f6ad12d",
"createInstant": 1622042134608,
"id": "f6e820d8-3482-4850-963c-d7bdeb04f588",
"registration": {
"applicationId": "b5d6a4a3-79ac-4413-a76b-f4432f6ad12d",
"id": "ad296086-c180-4593-b7b1-d81714b7b485",
"insertInstant": 1622042134606,
"lastLoginInstant": 1622042134606,
"lastUpdateInstant": 1622042134606,
"timezone": "America/Denver",
"username": "brian_ls@something.net",
"usernameStatus": "ACTIVE",
"verified": true
},
"tenantId": "fb3e777b-f716-2336-45c8-3173aa1713ae",
"user": {
"active": true,
"connectorId": "e3306678-a53a-4964-9040-1c96f36dda72",
"email": "brian_ls@something.net",
"firstName": "Brian",
"id": "7ea49170-3f58-4b9f-829d-99c2856d5004",
"insertInstant": 1620124501627,
"lastLoginInstant": 1622042134606,
"lastName": "Sellden",
"lastUpdateInstant": 1621966190205,
"middleName": "Lloyd",
"mobilePhone": "3035552294",
"passwordChangeRequired": false,
"passwordLastUpdateInstant": 1620124501700,
"tenantId": "fb3e777b-f716-2336-45c8-3173aa1713ae",
"timezone": "America/Denver",
"twoFactor": {},
"usernameStatus": "ACTIVE",
"verified": true
}
}
}Thank you - Brian.
-
@brian_ls I figured this out and wanted to put my solution here in case it's useful to someone.
There are 2 issues with the event string. First is that it is wrapped with an outer {}, and second it is wrapped with another outer field called 'event'. So I had to deal with each issue separately. I created a simple wrapper class:public class BaseEventWrapper {
BaseEvent event;public BaseEvent getEvent() { return event; }
}
So when I deserialize, I do so into this class. This takes care of the 'event' element. To remove the outermost {}, I set a property in the ObjectMapper, which all together looks like this:
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
BaseEvent event = mapper.readValue(newEvent, BaseEventWrapper.class).getEvent();Enjoy
Brian.
-
Thanks for the contribution! I am sure that it will be useful to others along the way!
Thanks,
Josh