我试图从聚合操作的响应中更改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的返回格式。
发布于 2019-02-04 21:59:14
我试着回答你的问题
我们需要定义您的op1响应类型
type typeOp1Response:any{
.format?:string
}或者如果你愿意
type typeOp1Response:undefined我个人更喜欢第一个,这样您就可以在聚合服务中决定mime。
现在您需要添加一个信使会话。
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(请求)(响应){
forward( request )( response );
if (request.format=="json"){
mime = "application/json"
};
if (request.format=="xml"){
mime = "application/xml"
};
} 不确定这是否能回答你的问题
发布于 2019-02-06 08:10:16
正如巴林特指出的那样,我们缺少一些关于答复性质的信息。
然而,在我看来,第二个例子更好地涵盖了一般情况。我们从来自聚合服务的任何信息(这忽略了它是聚合的事实)中抽象出来,并根据本地逻辑(在聚合器中)决定如何处理响应。
按照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的格式。
参考文献:
https://stackoverflow.com/questions/54524207
复制相似问题