簡體   English   中英

使用測試容器 pubsub 模擬器的反序列化問題

[英]Deserialization issue using test container pubsub emulator

我在使用 spring 應用程序進行集成測試時遇到問題。 我正在使用 testcontainer 來設置一個 pubsub 模擬器:

@Container
private static final PubSubEmulatorContainer pubsubEmulator =
    new PubSubEmulatorContainer(DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:317.0.0-emulators"));

@DynamicPropertySource
static void emulatorProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.cloud.gcp.pubsub.emulator-host", pubsubEmulator::getEmulatorEndpoint);
}

@Bean
CredentialsProvider googleCredentials() {
    return NoCredentialsProvider.create();
}

我正在發布這樣的消息:

publisherTemplate.publish("My_Topic", "{\"name\": \"test\"}", headers)

這是我對 pubsub 和 jackson 的應用程序配置:

@Configuration
@RequiredArgsConstructor
public class PubSubConsumerConfiguration {

    private final ConsumerRepository consumerRepository;

    @Bean
     public PubSubMessageConverter pubSubMessageConverter(ObjectMapper objectMapper) {
        return new JacksonPubSubMessageConverter(objectMapper);
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        return mapper;
    }

    @Bean
    public MessageChannel inputMessageChannel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public PubSubInboundChannelAdapter inboundChannelAdapter(MessageChannel inputMessageChannel, PubSubTemplate pubSubTemplate) {
        PubSubInboundChannelAdapter adapter = new 
        PubSubInboundChannelAdapter(pubSubTemplate, "my_sub");
        adapter.setOutputChannel(messageChannel);
        adapter.setAckMode(AckMode.MANUAL);
        adapter.setPayloadType(Consumer.class);
        return adapter;
    }

    @ServiceActivator(inputChannel = "inputMessageChannel")
    public void messageReceiver(
        @Payload Consumer payload,
        @Header(GcpPubSubHeaders.ORIGINAL_MESSAGE) BasicAcknowledgeablePubsubMessage message
    ) {

        consumerRepository.save(payload);
        message.ack();
    }
}

我正在定義一個 bean 來設置 jackson 作為從 pubsub 反序列化消息有效負載的主要方式。 在 GCP 上使用 pubsub 一切都很好,我在控制台中發送的有效負載被正確反序列化並保存到數據庫中。 但是,在我與 pubsub 的測試容器版本的集成測試中,我在反序列化過程中遇到了這個錯誤:

com.google.cloud.spring.pubsub.support.converter.PubSubMessageConversionException: 
JSON deserialization of an object of type com.mypackage.model.Consumer failed.; 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot construct instance of `com.mypackage.model.Consumer` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"name": "test"}')

我的測試方式有問題嗎?

編輯:正如@Ilya 所問,這里是 class com.mypackage.model.Consumer 的內容示例。我試過有無空的構造函數等。:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Consumer  {

    private String name;
    private ZonedDateTime deletedOn;
    ...
}

通過更深入地查看此文檔,我實際上找到了答案。 問題來自我在將消息發布到 pubsub 時指定有效負載的方式。

publisherTemplate.publish("My_Topic", ByteString.copyFromUtf8("{\"name\": \"test\"}"), headers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM