我正在为AWS SQS编写一些消息使用方,并希望通过使用javax.validation.constraints批注来验证收到的消息。 不幸的是,我不得不发现,使用过的spring-cloud-aws-messaging依赖关系的PayloadArgumentResolver使用了NoOpValidator。 因此,我尝试为有效负载注入自己的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解析标头。
@SqsListener(
value = "queue",
deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void consume(
@Payload @Validated final QueueMessage queueMessage,
@Headers final Map<String,Object> headers) {
}
当我仅将我的PayloadArgumentResolver与休眠验证器一起添加时,它也将用于解析标头doh!
有没有解决的办法,还是应该在spring-cloud-aws上发布一个问题?我只希望通过注释来验证我的有效载荷:(
答案 0 :(得分:0)
我认为这不是最好的 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();
}
}