我无法使用 HTTP 入站适配器将 JSON 数组转换为 SendgridTxEvent
类型的对象列表,它总是以 ArrayList<LinkedHashMap>
结尾而不是List<SendgridTxEvent>
。配置:
public HttpRequestHandlingMessagingGateway sendgridMessageAdapter(@Qualifier("sendgridWebhookEvents") MessageChannel channel) {
HttpRequestHandlingMessagingGateway httpInboundChannelAdapter = new HttpRequestHandlingMessagingGateway(false);
RequestMapping mapping = new RequestMapping();
mapping.setMethods(HttpMethod.POST);
mapping.setPathPatterns("/webhook/sendgrid");
ParameterizedTypeReference<List<SendgridTxEvent>> ptr = new ParameterizedTypeReference<List<SendgridTxEvent>>() {
};
httpInboundChannelAdapter.setRequestMapping(mapping);
httpInboundChannelAdapter.setRequestChannel(channel);
httpInboundChannelAdapter.setRequestPayloadType(ResolvableType.forType(ptr));
return httpInboundChannelAdapter;
}
如果我将请求负载类型设置为 httpInboundChannelAdapter.setRequestPayloadType(ResolvableType.forType(SendgridTxEvent.class))
并向其提供 JSON 对象(而不是数组),jackson 反序列化 SendgridTxEvent
正确,因此问题仅发生在数组输入时。输入示例可参见 here .
如何在 HTTP 入站适配器中使用 JSON 数组?
SendgridTxEvent
类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class SendgridTxEvent {
public enum Event {
PROCESSED,
DROPPED,
DELIVERED,
BOUNCE,
DEFERRED,
OPEN,
CLICK,
UNSUBSCRIBE,
SPAMREPORT;
@JsonCreator
public static Event forValue(String value) {
return Event.valueOf(value.toUpperCase());
}
}
private String email;
private Long timestamp;
private Event event;
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<String> category;
private String sgEventId;
private String sgMessageId;
//getters, setters
}
最佳答案
我提出了一个问题来在框架中解决此问题:https://github.com/spring-projects/spring-integration/issues/2806
同时,作为一种解决方法,我建议在 HttpRequestHandlingMessagingGateway
中期待有效负载如String
或byte[]
,然后使用 POJO @Transformer
下游通过ObjectMapper
直接转换已经符合您的预期 <List<SendgridTxEvent>
类型。
另一个简单的选项,您可以期待 SendgridTxEvent[].class
然后将其转换为下游列表。
关于java - HttpRequestHandlingMessagingGateway JSON 数组有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142118/