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