Skip to content

Error handling in JacksonJsonMessageConverter different from MappingJackson2MessageConverter #36424

@wimdeblauwe

Description

@wimdeblauwe

I am upgrading a project from Spring Boot 3.5 to Spring Boot 4. As part of the change, I changed configuring the MappingJackson2MessageConverter (which uses Jackson 2) to use JacksonJsonMessageConverter (which uses Jackson 3). I had some test failures after that in tests that check what happens if an invalid JMS message is put on the queue.

After some investigation, I noticed the following. This is the method body:

  @Override
  public Object fromMessage(Message message) throws JMSException, MessageConversionException {
    try {
      JavaType targetJavaType = getJavaTypeForMessage(message);
      return convertToObject(message, targetJavaType);
    }
    catch (IOException ex) {
      throw new MessageConversionException("Failed to convert JSON message content", ex);
    }
  }

Notice the catch(IOException). In Jackson 2, all exceptions thrown by Jackson are subclasses of IOException. So if the conversion cannot be done, the catch clause converts this to a MessageConversionException. This is also what my code expected to work properly.

With JacksonJsonMessageConverter, the code is still the same, but now Jackson 3 does not throw an IOException anymore. It now uses tools.jackson.core.JacksonException which is a RuntimeException.
As a result, there is no MessageConversionException anymore.

I can fix it on my side by catching JacksonException, as well as MessageConversionException, but I think it would be better if a catch for JacksonException is added that converts it to a MessageConversionException so the behaviour is the same as it was before.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: waiting-for-triageAn issue we've not yet triaged or decided on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions