0
votes

I use Jackson and hibernate in my spring-boot project:

private MappingJackson2HttpMessageConverter jacksonMessageConverter() {
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    messageConverter.setObjectMapper(mapper);
    return messageConverter;

}

And add it to converters:

public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(jacksonMessageConverter());

I use facebook webhooks to recive information about new messages in messenger:

public ResponseEntity<Void> handleCallback(@RequestBody final String payload,
        @RequestHeader(SIGNATURE_HEADER_NAME) final String signature) {

payload parameter is a string, but jackson converter cannot deserialize it :

w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: java.io.PushbackInputStream@60a118dc; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: java.io.PushbackInputStream@60a118dc; line: 1, column: 1]

I've tried to delete JacksonMapper and then webhooks work, but my database doesn't:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Do you have an idea how to deserialize facebook webhooks with jackson & hibernate?

1

1 Answers

0
votes

I understand you have a handler method for POST requests and you'd like spring-boot to deserialize the payload of the request into an object of a specified class, for example Message. In such a case, you need this syntax for handleCallback arguments:

@RequestBody final Message payload

And Jackson will create a Message object out of the payload, if the respective structures match (you may need extra configuration for Jackson).

With the current syntax, Jackson tries to deserialize the JSON payload into a String object and presumably the payload is more complicated than that.