gpt4 book ai didi

spring-cloud - 如何在 QueueMessageHandler 工作中对 spring-cloud-aws 进行 javax.validation 有效负载验证?

转载 作者:行者123 更新时间:2023-12-05 08:08:13 25 4
gpt4 key购买 nike

我正在为 AWS SQS 编写一些消息使用者,并希望使用 javax.validation.constraints 注释来验证接收到的消息。不幸的是,我不得不发现,spring-cloud-aws-messaging 依赖项中使用的 PayloadArgumentResolver 使用了 NoOpValidator。所以我尝试为有效负载注入(inject)我自己的 HandlerMethodArgumentResolver。

  @Bean
public QueueMessageHandlerFactory queueMessageHandlerFactory(
final ObjectMapper objectMapper, final Validator hibernateValidator) {
final MappingJackson2MessageConverter jacksonMessageConverter =
new MappingJackson2MessageConverter();
jacksonMessageConverter.setSerializedPayloadClass(String.class);
jacksonMessageConverter.setStrictContentTypeMatch(true);
jacksonMessageConverter.setObjectMapper(objectMapper);
final QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();

final List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
argumentResolvers.add(new HeaderMethodArgumentResolver(null, null));
argumentResolvers.add(new HeadersMethodArgumentResolver());

argumentResolvers.add(new NotificationSubjectArgumentResolver());
argumentResolvers.add(new AcknowledgmentHandlerMethodArgumentResolver("Acknowledgment"));
argumentResolvers.add(new VisibilityHandlerMethodArgumentResolver("Visibility"));

final PayloadArgumentResolver payloadArgumentResolver =
new PayloadArgumentResolver(jacksonMessageConverter, hibernateValidator);
argumentResolvers.add(payloadArgumentResolver);

factory.setArgumentResolvers(argumentResolvers);
return factory;
}

到目前为止一切顺利,乍一看,它运作良好...但是正如您所看到的,我还必须添加已经存在于 QueueMessageHandler 中的现有参数解析器,以通过消息的 @Headers/@Header 解析 header 。

  @SqsListener(
value = "queue",
deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void consume(
@Payload @Validated final QueueMessage queueMessage,
@Headers final Map<String,Object> headers) {
}

当我仅将我的 PayloadArgumentResolver 添加到休眠验证器时,它也将用于解析 header ,哦!

是否有任何漂亮的解决方案,或者我应该在 spring-cloud-aws 上打开一个问题?我只想通过注释验证我的有效载荷:(

最佳答案

我不认为这是最好的 awswer,但我有一个具有这种验证类型的工作示例项目:https://github.com/Haple/sqslistener

@Data
@RequiredArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
public class EventDTO {

@NotNull(message = "foo is mandatory")
private final String foo;

@NotNull(message = "bar is mandatory")
private final String bar;

}

@Slf4j
@Service
@AllArgsConstructor
public class SampleListener {

@SqsListener("test_queue")
public void execute(final @Valid @Payload EventDTO event) {
log.info("OK: {}", event);
}

}
@Configuration
public class MessageHandler {
@Bean
QueueMessageHandler queueMessageHandler(final AmazonSQSAsync amazonSQSAsync,
final MessageConverter messageConverter,
final Validator validator) {
final QueueMessageHandlerFactory queueMessageHandlerFactory = new QueueMessageHandlerFactory();
final PayloadMethodArgumentResolver payloadMethodArgumentResolver = new PayloadMethodArgumentResolver(messageConverter, validator);
queueMessageHandlerFactory.setArgumentResolvers(Collections.singletonList(payloadMethodArgumentResolver));
queueMessageHandlerFactory.setAmazonSqs(amazonSQSAsync);
queueMessageHandlerFactory.setMessageConverters(Collections.singletonList(messageConverter));
return queueMessageHandlerFactory.createQueueMessageHandler();
}
}

关于spring-cloud - 如何在 QueueMessageHandler 工作中对 spring-cloud-aws 进行 javax.validation 有效负载验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com