设置HTTP输出头参数可用于聚合

时间:2019-02-04 20:57:27

标签: http microservices jolie

我正在尝试从聚合操作的响应中更改contentType,这是我的示例代码。

interface MyAggregateInterface {
 RequestResponse:
 op1(typeOp1Request)(typeOp1Response)
}    
outputPort MyAggregatePort {
      Interfaces: MyAggregateInterface
    }

embedded {
      Jolie:
         "MyAggratedCode.ol" in MyAggregatePort
  }

 inputPort MyInputPortHttp {
    Protocol: http {
            .debug= 1;
            .debug.showContent =1;
            .format -> format;
            .contentType -> mime;
             .charset ="UTF-8";
            .default = "default";
            .compression = false
        }
        Location: "socket://localhost:8081"
        Interfaces: DefaultHttpInterface 
        Aggregates: MyAggregatePort
        }

我想更改op1的返回格式。

2 个答案:

答案 0 :(得分:1)

好吧,我会尽力回答您的问题

我们需要定义您的op1响应类型

type typeOp1Response:any{
  .format?:string
}

或者如果您愿意

type typeOp1Response:undefined

我个人更喜欢第一个,以便您可以确定聚合服务中的哑剧

现在您需要添加一个快递会议

courier MyInputPortHttp {
   [interface MyAggregateInterface( request )( response )]{

   forward( request )( response );
    if (is_defined(response.format)){
         mime = response.format;
         undef(response.format);
      }
    } 

此实现有一个限制,可以在根节点中返回平面数据 另一种方法是使用inputType定义输出格式。

type typeOp1Request:void{
  .otherParameter1:string
  .format?:string
}

然后您的快递员

快递MyInputPortHttp {        [接口MyAggregateInterface(request)(response)] {

   forward( request )( response );
    if (request.format=="json"){
         mime = "application/json"
      };
    if (request.format=="xml"){
         mime = "application/xml"
      };

    } 

不确定是否可以回答您的问题

答案 1 :(得分:1)

正如Balint所指出的,我们缺少有关回复性质的信息。

但是,在我看来,第二个示例更好地涵盖了一般情况。我们从聚合服务(忽略聚合的事实)中提取任何信息,然后根据本地逻辑(在聚合器中)决定如何处理响应。

按照Balint的示例,我们可以用courier包装聚合的操作,并在那里定义输出的格式。我在下面提供了一个最小的工作示例。

综合服务

type PersonRequestType: void {
  .name: string
}

type PersonResponseType: void {
  .name: string
  .surname: string
}

interface MyAggregatedInterface {
  RequestResponse: op1( PersonRequestType )( PersonResponseType ) throws RecordNotFound
}

inputPort IN {
  Location: "local"
  Interfaces: MyAggregatedInterface
}

execution { concurrent }

main
{
  op1( request )( response ){
    if( request.name == "Mario" ){
      response.name = "Mario";
      response.surname = "Mario"
    } else {
      throw ( RecordNotFound )
    }
  }
}

聚合器服务

include "aggregated.ol"

outputPort MyAggregatePort { Interfaces: MyAggregatedInterface }

embedded { Jolie: "aggregated.ol" in MyAggregatePort }

inputPort HttpPort {
  Location: "socket://localhost:8000"
  Protocol: http {
    .format -> format
  }
  Aggregates: MyAggregatePort
}

courier HttpPort {
 [ interface MyAggregatedInterface( request )( response ) ]{
    forward( request )( response );
    format = "json" // e.g., alternative xml
  }
}

通过将设置为format的值(例如,从"json"更改为"xml"),我们更改了HTTP响应的格式。

参考文献: