数据绑定失败

Data binding failed

我创建了一个新的芭蕾舞女演员类型,如下所示。

const string GENERAL="GENERAL";
const string SECURITY="SECURITY";

type INCIDENT_TYPE GENERAL|SECURITY;

public type incidentNotification record { 
    string title;
    INCIDENT_TYPE incidentType;

};

一旦我发送 POST 创建新事件通知的请求,sendIncident 方法就会被执行。

public function sendIncident (http:Request req, incidentNotification sendIncidentBody) returns http:Response {}

与请求正文一起发送的json有效负载如下。

{
    "title":"title",
    "incidentType": "SECURITY"

}

但是当我用 http 请求发送那个 json 时,它给出了错误

data binding failed: Error in reading payload : error while mapping 'incidentType': incompatible types: expected 'janaka/incident-reporting-service:0.0.1:$anonType|janaka/incident-reporting-service:0.0.1:$anonType', found 'string'

如何解决这个错误?

这是 JSON 到 Record 转换中的一个错误,已在 ballerina 最新版本中修复(v1.0.0-alpha)

import ballerina/http;
import ballerina/log;

const string GENERAL="GENERAL";
const string SECURITY="SECURITY";

type INCIDENT_TYPE GENERAL|SECURITY;

public type IncidentNotification record { 
    string title;
    INCIDENT_TYPE incidentType;
};

service hello on new http:Listener(9090) {
    @http:ResourceConfig {
        methods: ["POST"],
        body: "notification"
    }
    resource function bindJson(http:Caller caller, http:Request req, 
                                      IncidentNotification notification) {
        var result = caller->respond(notification.title);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
}

以上示例资源适用于以下请求

curl -v http://localhost:9090/hello/bindJson -d '{ "title": "title" , "incidentType" : "SECURITY"}' -H "Content-Type:application/json"

回复:

< HTTP/1.1 200 OK
< content-type: text/plain
< content-length: 5
< 
* Connection #0 to host localhost left intact
title