猿问

配置 Java AWS Lambda 使用的 Objectmapper

我正在尝试开发一个由来自 SQS 的事件触发的 AWS Lambda 函数。


我正在使用spring-cloud-function-adapter-aws(版本 1.0.0.RELEASE),特别是SpringBootRequestHandler。


但是,正在使用的 ObjectMapper 区分大小写,因此无法成功转换来自 SQS 的 Json。


SQS 发布了以下 Json,尤其是我遇到了问题的Records字段。


    {

  "Records": [

    {

      "body": "Hello from SQS!",

      "receiptHandle": "MessageReceiptHandle",

      "md5OfBody": "7b270e59b47ff90a553787216d55d91d",

      "eventSourceARN": "arn:aws:sqs:eu-west-1:123456789012:MyQueue",

      "eventSource": "aws:sqs",

      "awsRegion": "eu-west-1",

      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",

      "attributes": {

        "ApproximateFirstReceiveTimestamp": "1523232000001",

        "SenderId": "123456789012",

        "ApproximateReceiveCount": "1",

        "SentTimestamp": "1523232000000"

      },

      "messageAttributes": {}

    }

  ]

}

我已经尝试过这个问题中的建议,但无济于事。在 Spring 中配置 ObjectMapper


在我的 POJO 中,我还添加了以下注释,但它在 Lambda 之外也不起作用。


@JsonProperty("Records")

private List<SqsRecord> Records;

任何帮助将非常感激。


我的 Lambda 处理程序定义为:


public class SqsEventHandler extends SpringBootRequestHandler<SqsEvent, String> {}

POJO 定义为:


public class SqsEvent {


@JsonProperty("Records")

private List<SqsRecord> records;


@Data

public class SqsRecord {

    private String body;

    private String receiptHandle;

    private String md5OfBody;

    private String eventSourceARN;

    private String eventSource;

    private String awsRegion;

    private String messageId;

}

}


我希望 ObjectMapper 能够读入示例消息中的 Json,但“记录”字段为空。


MMMHUHU
浏览 206回答 2
2回答

侃侃尔雅

我们在很多 AWS 服务中都遇到了这个问题。您必须像这样定义一个新的映射器:SQSMixin :&nbsp;private static interface SQSEventMixin {&nbsp; &nbsp; public static final String ATTRIBUTES = "attributes";&nbsp; &nbsp; public static final String AWS_REGION = "awsRegion";&nbsp; &nbsp; public static final String BODY = "body";&nbsp; &nbsp; public static final String EVENT_SOURCE = "eventSource";&nbsp; &nbsp; public static final String EVENT_SOURCE_ARN = "eventSourceARN";&nbsp; &nbsp; public static final String MD5_OF_BOBY = "md5OfBody";&nbsp; &nbsp; public static final String MD5_OF_MESSAGE_ATTRIBUTES = "md5OfMessageAttributes";&nbsp; &nbsp; public static final String MESSAGE_ID = "messageId";&nbsp; &nbsp; public static final String RECEIPT_HANDLE = "receiptHandle";&nbsp; &nbsp; @JsonProperty(value = "Records")&nbsp; &nbsp; public List<?> getRecords();&nbsp; &nbsp; static interface MessageMixin {&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(ATTRIBUTES)&nbsp; &nbsp; &nbsp; &nbsp; public String getAttributes();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(ATTRIBUTES)&nbsp; &nbsp; &nbsp; &nbsp; public void setAttributes(String attributes);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(AWS_REGION)&nbsp; &nbsp; &nbsp; &nbsp; public String getAwsRegion();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(AWS_REGION)&nbsp; &nbsp; &nbsp; &nbsp; public void setAwsRegion(String awsRegion);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(BODY)&nbsp; &nbsp; &nbsp; &nbsp; public Object getBody();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(BODY)&nbsp; &nbsp; &nbsp; &nbsp; public void setBody(Object body);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(EVENT_SOURCE)&nbsp; &nbsp; &nbsp; &nbsp; public String getEventSource();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(EVENT_SOURCE)&nbsp; &nbsp; &nbsp; &nbsp; public void setEventSource(String eventSource);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(EVENT_SOURCE_ARN)&nbsp; &nbsp; &nbsp; &nbsp; public String getEventSourceArn();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(EVENT_SOURCE_ARN)&nbsp; &nbsp; &nbsp; &nbsp; public void setEventSourceArn(String eventSourceArn);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MD5_OF_BOBY)&nbsp; &nbsp; &nbsp; &nbsp; public String getMd5OfBody();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MD5_OF_BOBY)&nbsp; &nbsp; &nbsp; &nbsp; public void setMd5OfBody(String md5OfBody);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MD5_OF_MESSAGE_ATTRIBUTES)&nbsp; &nbsp; &nbsp; &nbsp; public String getMd5OfMessageAttributes();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MD5_OF_MESSAGE_ATTRIBUTES)&nbsp; &nbsp; &nbsp; &nbsp; public void setMd5OfMessageAttributes(String md5OfMessageAttributes);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MESSAGE_ID)&nbsp; &nbsp; &nbsp; &nbsp; public String getMessageId();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(MESSAGE_ID)&nbsp; &nbsp; &nbsp; &nbsp; public void setMessageId(String messageId);&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(RECEIPT_HANDLE)&nbsp; &nbsp; &nbsp; &nbsp; public String getReceiptHandle();&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty(RECEIPT_HANDLE)&nbsp; &nbsp; &nbsp; &nbsp; public void setReceiptHandle(String receiptHandle);&nbsp; &nbsp; }}记录策略:private static class UpperCaseRecordsPropertyNamingStrategy extends PropertyNamingStrategy.PropertyNamingStrategyBase {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String translate(String propertyName) {&nbsp; &nbsp; &nbsp; &nbsp; if (propertyName.equals("records")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Records";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return propertyName;&nbsp; &nbsp; }}日期格式化程序:private static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime()&nbsp; &nbsp; &nbsp; &nbsp; .withZone(new FixedDateTimeZone("GMT", "GMT", 0, 0));private static class DateTimeMapperModule extends SimpleModule {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; public DateTimeMapperModule() {&nbsp; &nbsp; &nbsp; &nbsp; super("DateTimeMapperModule");&nbsp; &nbsp; &nbsp; &nbsp; super.addSerializer(DateTime.class, new DateTimeSerializer());&nbsp; &nbsp; &nbsp; &nbsp; super.addDeserializer(DateTime.class, new DateTimeDeserializer());&nbsp; &nbsp; }}private static class DateTimeSerializer extends JsonSerializer<DateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(DateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; gen.writeString(dateTimeFormatter.print(value));&nbsp; &nbsp; }}private static class DateTimeDeserializer extends JsonDeserializer<DateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public DateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; return dateTimeFormatter.parseDateTime(parser.getText());&nbsp; &nbsp; }}并声明您的映射器:&nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);&nbsp; &nbsp; mapper.setPropertyNamingStrategy(new UpperCaseRecordsPropertyNamingStrategy());&nbsp; &nbsp; mapper.registerModule(new DateTimeMapperModule());&nbsp; &nbsp; mapper.addMixIn(SQSMessage.class, SQSEventMixin.MessageMixin.class);&nbsp; &nbsp; SQSEvent request = mapper.convertValue(inputObject, SQSEvent.class);
随时随地看视频慕课网APP

相关分类

Java
我要回答