JSON in Objekt umwandeln

internet

Top Contributor
Ich versuche gerade einen Webhook zu empfangen (javax.ws.rs) und diesen auszulesen:

Java:
@Stateless
@Path("/")
@Hidden
public class WebhookBean {


    /**
     * Erhält das Event
     *
     * @return
     */
    @POST
    @Path("/webhook")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getEvent(String json) {

        try {

            Event event = (Event) JsonHelper.createJsonObject(json, Event.class, null);
            
            EventType eventType = event.eventType();
              
            ......
        }

        catch (Exception e) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }

        return Response.ok().build();

    }

Code:
public class JsonHelper {

    /**
     * Erstellt ein JSON String von einem Objekt
     *
     * @return
     * @throws JsonMappingException
     * @throws JsonProcessingException
     */
    public static Object createJsonObject(String jsonString, Class clazz, List<Class> registerSubTypeList)
            throws JsonMappingException, JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();

        if (registerSubTypeList != null) {
            for (Object object : registerSubTypeList) {
                objectMapper.registerSubtypes(object.getClass());
            }
        }

        @SuppressWarnings("unchecked")
        Object objectOutput = objectMapper.readValue(jsonString, clazz);

        return objectOutput;
    }

}

Nun bekomme ich aber als Fehlermeldung:

Cannot construct instance of com.chargebee.models.Event (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)


at [Source: (String)"{"id":"ev3XpbG6hnQqGFlw73J","occurred_at":1341085213,"source":"api","user":"full_access_key_v1","object":"event","api_version":"v2","content":{"plan":{"id":"package","name":"Package","price":1000,"period":1,"period_unit":"month","charge_model":"per_unit","free_quantity":0,"status":"active","enabled_in_hosted_pages":true,"enabled_in_portal":true,"updated_at":1341085212,"resource_version":1341085212000,"object":"plan","taxable":true,"currency_code":"USD"}},"event_type":"plan_created","webhook_stat"[truncated 21 chars]; line: 1, column: 2]

Auslesen will ich das Object "Event":

Was mache ich denn falsch?
 

Neue Themen


Zurück
Oben