首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置处理聚合的HTTP输出标头参数

设置处理聚合的HTTP输出标头参数
EN

Stack Overflow用户
提问于 2019-02-04 20:57:26
回答 2查看 52关注 0票数 2

我试图从聚合操作的响应中更改contentType,下面是我的示例代码。

代码语言:javascript
运行
复制
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的返回格式。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-04 21:59:14

我试着回答你的问题

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

代码语言:javascript
运行
复制
type typeOp1Response:any{
  .format?:string
}

或者如果你愿意

代码语言:javascript
运行
复制
type typeOp1Response:undefined

我个人更喜欢第一个,这样您就可以在聚合服务中决定mime。

现在您需要添加一个信使会话。

代码语言:javascript
运行
复制
courier MyInputPortHttp {
   [interface MyAggregateInterface( request )( response )]{

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

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

代码语言:javascript
运行
复制
type typeOp1Request:void{
  .otherParameter1:string
  .format?:string
}

然后你的信使

信使MyInputPortHttp {接口MyAggregateInterface(请求)(响应){

代码语言:javascript
运行
复制
   forward( request )( response );
    if (request.format=="json"){
         mime = "application/json"
      };
    if (request.format=="xml"){
         mime = "application/xml"
      };

    } 

不确定这是否能回答你的问题

票数 2
EN

Stack Overflow用户

发布于 2019-02-06 08:10:16

正如巴林特指出的那样,我们缺少一些关于答复性质的信息。

然而,在我看来,第二个例子更好地涵盖了一般情况。我们从来自聚合服务的任何信息(这忽略了它是聚合的事实)中抽象出来,并根据本地逻辑(在聚合器中)决定如何处理响应。

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

聚合服务

代码语言:javascript
运行
复制
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 )
    }
  }
}

聚合器服务

代码语言:javascript
运行
复制
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的格式。

参考文献:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54524207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档