简体   繁体   English

接受多个输入并以相同方式处理它们的最佳实践

[英]Best practice for accepting multiple inputs and processing them the same way

I am building an application that will be accepting requests from different clients and sending them to a single output. 我正在构建一个将接受来自不同客户端的请求并将其发送到单个输出的应用程序。 These clients will be sending different JSON requests that ultimately need to be transformed and then submitted to a final service. 这些客户端将发送最终需要转换然后提交给最终服务的不同JSON请求。 An example of what I mean.. 我的意思的例子

ClientA posts ObjectA as JSON to endpoint /clientA. ClientA将ObjectA作为JSON发布到端点/ clientA。 This request is transformed into an object FinalObject and submitted to a work queue via ServiceA that is processed and sent to FinalEndpoint. 该请求被转换为对象FinalObject,并通过ServiceA提交到工作队列,该队列被处理并发送到FinalEndpoint。

ClientB posts ObjectB as JSON to endpoint /clientB. ClientB将ObjectB作为JSON发布到端点/ clientB。 This request is transformed into an object FinalObject and submitted to a work queue via ServiceA that is processed and sent to FinalEndpoint. 该请求被转换为对象FinalObject,并通过ServiceA提交到工作队列,该队列被处理并发送到FinalEndpoint。

I would like to avoid duplicating the code as much as possible. 我想避免重复代码。 I'm really not sure what the best way to do this is. 我真的不确定什么是最好的方法。 The fields in ObjectA and ObjectB are completely different and need to be formatted and calculated to derive FinalObject. ObjectA和ObjectB中的字段完全不同,需要进行格式化和计算以得出FinalObject。 I imagine I'll need some kind of Transformer to convert to the appropriate object I'm just not sure what pattern to use. 我想我需要某种Transformer才能转换为适当的对象,但我不确定该使用哪种模式。

Any help is GREATLY appreciated. 任何帮助是极大的赞赏。 Thank you. 谢谢。

The most general solution: you define endpoint (if you are using springMVC it can be /process/${eventType}/) that accept some json body. 最通用的解决方案:您定义接受某些json主体的终结点(如果使用的是springMVC,则可以为/ process / $ {eventType} /)。 Based on eventType (it can be part of url or urlParam) you decide what convertor to use for json. 基于eventType(它可以是url或urlParam的一部分),您可以决定对json使用哪个转换器。

Map<String, JsonConvertor> converters = new HashMap<>();
//init convertos map with possible cases you want to handle

//in controller method
outputResult = converters.get(eventType).convert(inputJson)
sendResult(ouputResult)

I would create a common Interface that is implemented by objectA and ObjectB, the interface could contain both objectA and objectB fields, let the interface be ObjectX, each implementation could contain the transformation method that returns a FinalObject and do the transformation differently for ObjectA and ObjectB, this is an example: 我将创建一个由objectA和ObjectB实现的通用接口,该接口可以同时包含objectA和objectB字段,让该接口为ObjectX,每个实现都可以包含返回FinalObject的转换方法,并对ObjectA和ObjectB进行不同的转换,这是一个示例:

1- FinalObject class 1- FinalObject类

public class FinalObject {
   // FinalObject implementation here 
}

2- Interface 2-接口

interface ObjectX {
    public FinalObject getFinalObject();
  // define any other common methods that you might need for both objects
}

3- ObjectA 3-对象A

public class ObjectA implements ObjectX {
    // define any special fields
    @override
    public FinalObject getFinalObject(){
         // implement conversion of ObjectA to FinalObject here 
    }
// implement any other methods you need 
}

4- ObjectB 4-对象B

 public class ObjectB implements ObjectX {
    // define any special fields
    @override
    public FinalObject getFinalObject(){
         // implement conversion of ObjectB to FinalObject here 
    }
// implement any other methods you need 
}

and for the service it could be a single service distinguish which implementation to use depending on the JSON values, or you can use different paths/parameters for each client to access a different method on your service controller. 对于服务,它可以是单个服务,根据JSON值区分使用哪种实现,也可以为每个客户端使用不同的路径/参数来访问服务控制器上的不同方法。 I hope that this is the answer you are looking for. 我希望这是您正在寻找的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM