【问题标题】:AWS lambda return modified input object as a result结果 AWS lambda 返回修改后的输入对象
【发布时间】:2022-12-10 00:23:58
【问题描述】:

通常你需要用InputPath, ResultPath and OutputPath做一些事情来处理你的数据。
但有时您需要像修改输入消息一样简单的操作,例如 camel doMessage EIPEnricher EIP
这不是我能够轻易实现的。有什么解决办法?

【问题讨论】:

    标签: java amazon-web-services aws-lambda integration-patterns


    【解决方案1】:

    考虑 Jackson tree model 的方法:

    public class MyHandler extends JsonHandler<ObjectNode> {
    
        @Override
        public ObjectNode handleRequest(ObjectNode json, Context context) {
            // enrich the input
            json.putPOJO("enriched", pojo);
            // return (the same) modified input object
            return json;
            // or replace it with something totally different
            // return "Some other POJO";
        }
    }
    

    其中 JsonHandler 父类定义为

    public abstract class JsonHandler<R> implements RequestStreamHandler, RequestHandler<ObjectNode, R> {
        
        private ObjectMapper objectMapper = new ObjectMapper();
    
        @Override
        public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
            ObjectNode json = (ObjectNode) objectMapper.readTree(input);
            R result = handleRequest(json, context);
            output.write(objectMapper.writeValueAsString(result).getBytes());
        }
    
        @Override
        public abstract R handleRequest(ObjectNode json, Context context);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2020-09-18
      • 1970-01-01
      • 2020-06-14
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多