【问题标题】:Spring Integration - MessageTransformationException - MismatchedInputException: Cannot deserialize instance of StringSpring Integration - MessageTransformationException - MismatchedInputException:无法反序列化字符串实例
【发布时间】:2020-07-21 06:23:50
【问题描述】:

最近将我的项目从 2.1.X

更新为 Spring boot 到 2.2.X

更新项目后遇到异常。

我正在使用 spring 提供的 JSON 转换器将 json 字符串转换为对象。

myproject-context.xml

<int-amqp:inbound-channel-adapter id="updateProductDetailsQueueAdapter"
            channel="updateProductDetailsQueueChannel" queue-names="update-product-details-queue"
            connection-factory="rabbitConnectionFactory"/>
            
<int:json-to-object-transformer
    input-channel="updateProductDetailsQueueChannel"
    output-channel="updateProductDetailsTransformerInputChannel" type="org.somePackage.UpdateProductDetailsEvent" />

<int:transformer
    input-channel="updateProductDetailsTransformerInputChannel"
    output-channel="updateProductDetailsTransformerOutputChannel" expression="payload.getUpdateMessage()" />

模型类

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
@EqualsAndHashCode
public class UpdateProductDetailsEvent implements Serializable {
  private static final long serialVersionUID = 1L;
  private String productType;
  private String updateMessage;

  @JsonCreator
  public UpdateProductDetailsEvent(@JsonProperty("productType") String productType, @JsonProperty("updateMessage") String updateMessage) {
    this.productType = productType;
    this.updateMessage = updateMessage;
   }
}
  Caused by:
  org.springframework.integration.transformer.MessageTransformationException: failed to
  transform message; nested exception is java.io.UncheckedIOException: 
  com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of 
  `java.lang.String` out of START_OBJECT token
  at [Source: (String)"{"productType":"Electronics","updateMessage":"Description to update"}"; line: 1, column: 1]
  .........................
  .........................
  Caused by:
  java.io.UncheckedIOException: 
  com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of 
  `java.lang.String` out of START_OBJECT token
  at [Source: (String)"{"productType":"Electronics","updateMessage":"Description to update"}"; line: 1, column: 1] at 
  org.springframework.integration.json.JsonToObjectTransformer.doTransform
  (JsonToObjectTransformer.java:132) ~[spring-integration-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]

【问题讨论】:

  • 有没有机会和我们分享一个简单的 Spring Boot 项目来重现和使用?

标签: java spring-boot spring-integration spring-amqp


【解决方案1】:

最后,我找到了解决方案,问题是由于输入消息中不存在content-type 属性。 因此,如果消息中不存在标头,则 object-to-json-transformer 将 content-type 设置为 application/json

我已经用空字符串(如content-type="")抑制了设置

For more details - Read this spring blog

<!-- My Out bound Queue configuration -->
<int:object-to-json-transformer input-channel="productDetailsToUpdate"
    output-channel="outnoundChannel"  
    content-type="" />

<int-amqp:outbound-channel-adapter
            id="outboundAdapterId" amqp-template="rabbitTemplate"
            channel="outboundDataExchange" exchange-name="update-product-details-exchange" /> 

<!-- My In bound Queue configuration -->
<int-amqp:inbound-channel-adapter id="updateProductDetailsQueueAdapter"
            channel="updateProductDetailsQueueChannel" queue-names="update-product-details-queue"
            connection-factory="rabbitConnectionFactory"/>
            
<int:json-to-object-transformer
    input-channel="updateProductDetailsQueueChannel"
    output-channel="updateProductDetailsTransformerInputChannel" 
    type="org.somePackage.UpdateProductDetailsEvent" />

<int:transformer input-channel="updateProductDetailsTransformerInputChannel"
    output-channel="updateProductDetailsTransformerOutputChannel" 
    expression="payload.getUpdateMessage()" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2019-09-17
    • 2018-10-22
    • 1970-01-01
    • 2019-07-06
    • 2012-12-28
    • 1970-01-01
    相关资源
    最近更新 更多