AWS SNS http 订阅返回“不支持的媒体类型:不支持内容类型‘text/plain;charset=UTF-8’”

问题描述 投票:0回答:2

我有一个 SNS 主题和一个 HTTP 订阅,它们调用 Spring boot 端点,我的端点如下所示:

@RestController
public class MyController {

    @PostMapping(value = "/path")
    public ResponseEntity<Void> action(@RequestBody Object body) {
        return ResponseEntity.noContent().build();
    }
}

当我使用 AWS CLI 在本主题中发布消息时,我尝试调用此端点,如下所示:

aws sns publish --topic-arn arn:aws:sns:eu-west-1:000000000000:MyTopic \
    --message '{"name": "test"}' --endpoint-url http://localhost:4566

但是我得到一个错误:

Unsupported Media Type: Content type 'text/plain;charset=UTF-8' is not supported

我将

MediaType.TEXT_PLAIN_VALUE
添加到我的代码中:

@PostMapping(value = "/path", consumes = MediaType.TEXT_PLAIN_VALUE)

但是我得到了同样的错误。

出于某种原因,主题使用不同的

text/plain
媒体类型发送消息。

我学到了这个文档,并且有效地使用以下方式发送消息:

...
Content-Type: text/plain; charset=UTF-8
...

我的问题是有什么办法可以强制 SNS 发布者使用

application/json
?

或者有什么方法可以解决我的控制器中的问题吗?

java amazon-web-services spring-boot amazon-sns
2个回答
0
投票

当我使用

MediaType.TEXT_PLAIN_VALUE
@RequestBody String body

时问题解决了
@PostMapping(value = "/path", consumes = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> action(@RequestBody String body) {

0
投票

Amazon SNS 现在支持从主题传递的 HTTP 消息的自定义

Content-Type
标头。这是公告:https://aws.amazon.com/about-aws/whats-new/2023/03/amazon-sns-content-type-request-headers-http-s-notifications/

您只需修改 Amazon SNS 订阅的

DeliveryPolicy
属性,将
headerContentType
属性设置为
application/json
application/xml
或支持的任何其他值。您可以在此处找到所有支持的值:https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html#creating-delivery-policy

{
    "healthyRetryPolicy": {
        "minDelayTarget": 1,
        "maxDelayTarget": 60,
        "numRetries": 50,
        "numNoDelayRetries": 3,
        "numMinDelayRetries": 2,
        "numMaxDelayRetries": 35,
        "backoffFunction": "exponential"
    },
    "throttlePolicy": {
        "maxReceivesPerSecond": 10
    },
    "requestPolicy": {
        "headerContentType": "application/json"
    }
}

您通过调用

DeliveryPolicy
API 操作来设置
SetSubscriptionAttributes
属性:https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html

© www.soinside.com 2019 - 2024. All rights reserved.