在 Spring Cloud for AWS SNS 中,在收到 SNS 消息时抛出 java.lang.IllegalArgumentException

In Spring Cloud for AWS SNS throwing java.lang.IllegalArgumentException on receiving SNS message

我得到异常:

2021-06-16 05:28:57.497 ERROR 2751 --- [nio-8081-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: @NotificationMessage annotated parameters are only allowed for method that receive a notification message.] with root cause

java.lang.IllegalArgumentException: @NotificationMessage annotated parameters are only allowed for method that receive a notification message.
        at org.springframework.cloud.aws.messaging.endpoint.NotificationSubjectHandlerMethodArgumentResolver.doResolverArgumentFromNotificationMessage(NotificationSubjectHandlerMethodArgumentResolver.java:39) ~[spring-cloud-aws-messaging-1.0.0.RELEASE.jar!/:1.0.0.RELEASE]
        at org.springframework.cloud.aws.messaging.endpoint.AbstractNotificationMessageHandlerMethodArgumentResolver.resolveArgument(AbstractNotificationMessageHandlerMethodArgumentResolver.java:49) ~[spring-cloud-aws-messaging-1.0.0.RELEASE.jar!/:1.0.0.RELEASE]
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]

主题订阅成功。

应该处理消息的方法代码(Java SpringCloud)

    @NotificationMessageMapping
    public void handleNotificationMessage(@NotificationSubject String subject, @NotificationMessage String message) {
        System.out.println("in handleNotificationMessage(): " + message + ", " + subject);
        System.out.println("timestamp: "+ System.currentTimeMillis());
    }

Python 发布消息的代码

        client = boto3.client(
            "sns",
            aws_access_key_id=Constants.environment_properties().AWS_ACCESS_KEY_ID,
            aws_secret_access_key=Constants.environment_properties().AWS_SECRET_ACCESS_KEY,
            region_name=Constants.environment_properties().REGION_NAME
        )
        message = {
            "s3_file_key": s3_object_key,
            "file_timestamp": file_timestamp,
            "user_id": user_json["id"],
            "username": user_json["username"],
            "device_id": user_json["device_id"]
        }
        # Send your message.
        client.publish(
            TopicArn=Constants.environment_properties().SNS_TOPIC_ARN,
            Message=json.dumps({'default': json.dumps(message)}),
            Subject="Raw data",
            MessageStructure='json'
        )

我也遇到了同样的问题。 请在您创建的主题订阅中禁用“原始消息传递”进行检查。 我不知道确切原因,但它对我有用