title: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
body: <p>I use Spring Boot and Rappitmq so realize async messaging between two different applications. This works fine when I only exchange Strings as message content. I also imlemented the exchange of pojos. Because of the pojos beeing in different packages the serialization/deserialization process fails. To solve that  I use the JacksonConverter to use json. This works as I can see in the message header in the subscriber service &quot;application/json&quot;</p>
  <p><strong>Model to exchange (Same in publish- and subscriber service):</strong></p>
  <pre><code>public class Employee{
  
  private Integer id;
  private String firstName;
  private String lastName;
  private Double salary;
  
  public Employee(@JsonProperty(&quot;id&quot;) Integer id,
                  @JsonProperty(&quot;firstName&quot;) String firstName,
                  @JsonProperty(&quot;lastName&quot;) String lastName,
                  @JsonProperty(&quot;salary&quot;) Double salary) {
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
      this.salary = salary;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getFirstName() {
      return firstName;
  }
  
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  public String getLastName() {
      return lastName;
  }
  
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  public Double getSalary() {
      return salary;
  }
  
  public void setSalary(Double salary) {
      this.salary = salary;
  }
  
  @Override
  public String toString() {
      return &quot;{&quot; +
              &quot;id=&quot; + id +
              &quot;, firstName='&quot; + firstName + '\'' +
              &quot;, lastName='&quot; + lastName + '\'' +
              &quot;, salary=&quot; + salary +
              '}';
  }
  </code></pre>
  <p><strong>Beans to use Jackson2JsonConverter in publisher service:</strong></p>
  <pre><code> @SpringBootApplication
  public class PublisherApplication {
  
  public static void main(String[] args) {
  
      SpringApplication.run(PublisherApplication.class, args);
  
  }
  
  @Bean
  public Jackson2JsonMessageConverter producerMessageConverter(){
      return new Jackson2JsonMessageConverter();
  }
  
  @Bean
  public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory){
      RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
      rabbitTemplate.setMessageConverter(producerMessageConverter());
      return rabbitTemplate;
  }
  </code></pre>
  <p>}</p>
  <p><strong>Send Message:</strong></p>
  <pre><code>    @Override
  public void sendEmployeeObject(Employee employee) throws JsonProcessingException {
      rabbitTemplate.convertAndSend(RabbitMqConstants.EXCHANGE_NAME, &quot;foo.bar.baz&quot;, employee);
  }
  </code></pre>
  <p>}</p>
  <p><strong>Subscriber config:</strong></p>
  <pre><code>   @Configuration
  public class EmployeeMessageSubscriberConfig {
  
  @Bean
  @Qualifier(&quot;employeeQueue&quot;)
  Queue queue() {
      return new Queue(RabbitMqConstants.EMPLOYEE_QUEUE, false);
  }
  
  @Bean
  @Qualifier(&quot;employeeExchange&quot;)
  TopicExchange exchange() {
      return new TopicExchange(RabbitMqConstants.EXCHANGE_NAME);
  }
  
  @Bean
  @Qualifier(&quot;employeeQueueExchangeBinding&quot;)
  Binding binding(Queue queue, TopicExchange exchange) {
      return BindingBuilder.bind(queue).to(exchange).with(&quot;foo.bar.#&quot;);
  }
  @Bean
  public Jackson2JsonMessageConverter converter() {
      return new Jackson2JsonMessageConverter();
  }
  </code></pre>
  <p>}</p>
  <p><strong>Listener Method:</strong></p>
  <pre><code> @RabbitListener(queues = RabbitMqConstants.EMPLOYEE_QUEUE)
      public void handleMessage(Employee employee) {
  
          System.out.println(&quot;Received &lt;&quot; + employee.toString() + &quot;&gt;&quot;);
      }
  </code></pre>
  <p>To test it I send an Employee object via rest api to the publisher service. Here I just pass it unchanged to rabbit. The subscriber service reads the message und just prints it to the console.</p>
  <p><strong>The Problem:</strong>
  The subscriber service prints the correct value:</p>
  <pre><code>Received &lt;{id=1, firstName='John', lastName='Doe', salary=1.5}&gt;
  </code></pre>
  <p>but I get also an error trace:</p>
  <pre><code>    Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
   at [Source: (String)&quot;{&quot;id&quot;:1,&quot;firstName&quot;:&quot;John&quot;,&quot;lastName&quot;:&quot;Doe&quot;,&quot;salary&quot;:1.5}&quot;; line: 1, column: 1]
      at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) 
  </code></pre>
  <p>So as I understand the subscriber sees that the incoming Json is ob type Object but it tries to deserialize it as a String.</p>
  <p><strong>Question:</strong>
  Why is the the correct output printed when the trace says that there seems a to be an error?
  When I use a RestController, the incoming Object is mapped automatically to the given Model. Do I have to wire this up some somehow when using rabbitmq?</p>
  <p><strong>EDIT:</strong></p>
  <p>That's the <a href="https://thepracticaldeveloper.com/2016/10/23/produce-and-consume-json-messages-with-spring-boot-amqp/" rel="nofollow noreferrer">guide</a> I followed.</p>
  <p>As I've seen in the header in my subscriber, there is field <code>headers={__TypeId__=com.publisher.model.Employee}</code> so I've set the Employee model in both applications to a package with the same name. Still same error. ( Like I expected because contrary to the standardMessageConverter the packe name is not used for serialization )</p>