Wso2 API经理。URL没有版本号时出现异常

Wso2 API经理。URL没有版本号时出现异常,wso2,wso2-am,Wso2,Wso2 Am,我使用的是WSO2 API管理器1.10.0。若我发布API并订阅,然后用版本号调用这个API,一切都正常。已选中API的默认版本。当我向此API发送请求时,URL中没有版本,我得到异常: TID: [-1234] [] [2016-05-06 07:23:01,782] ERROR {org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler} - Cannot publish event. null {org.wso2.carbon.

我使用的是WSO2 API管理器1.10.0。若我发布API并订阅,然后用版本号调用这个API,一切都正常。已选中API的默认版本。当我向此API发送请求时,URL中没有版本,我得到异常:

TID: [-1234] [] [2016-05-06 07:23:01,782] ERROR {org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler} -  Cannot publish event. null {org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler}
java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:552)
at java.lang.Long.parseLong(Long.java:631)
at org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler.mediate(APIMgtFaultHandler.java:76)
at    org.apache.synapse.mediators.ext.ClassMediator.mediate(ClassMediator.java:84)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:155)

at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:220)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:155)
at org.apache.synapse.rest.API.process(API.java:389)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:97)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:65)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:295)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:317)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:149)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我们的API管理器配置为与WSO2 is 5.1.0和WSO2 DAS一起工作。

我已经测试了您的用例,但对我来说,它工作正常。在选中“将此设置为默认版本”复选框后,请再次检查该API是否正确保存和发布。 您可以通过在carbon控制台中列出API(元数据->列表->API)来检查它,然后单击API名称

我只是使用带有一些查询参数的CalculatorAPI示例对其进行了测试。我刚刚发布了样本,并订阅了计算器API。以下是调查结果

发布之后(不勾选使其成为默认版本复选框)

版本为的第一次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
无版本的第二次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
无版本的第二次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}

我已经测试了您的用例,但对我来说,它运行良好。在选中“将此设置为默认版本”复选框后,请再次检查该API是否正确保存和发布。 您可以通过在carbon控制台中列出API(元数据->列表->API)来检查它,然后单击API名称

我只是使用带有一些查询参数的CalculatorAPI示例对其进行了测试。我刚刚发布了样本,并订阅了计算器API。以下是调查结果

发布之后(不勾选使其成为默认版本复选框)

版本为的第一次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
无版本的第二次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}
无版本的第二次API调用

curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/1.0/add?x=2&y=2"
curl -k -X GET --header "Accept: application/json" --header "Authorization: Bearer afbbb795dadf272cec9c44efc78dc81b" "https://192.168.1.4:8243/calc/add?x=2&y=2"
响应

{"answer": "4.0"}
am:fault xmlns:am="http://wso2.org/apimanager"><am:code>404</am:code><am:type>Status report</am:type><am:message>Not Found</am:message><am:description>The requested resource (/calc/add?x=2&amp;y=2) is not available.</am:description></am:fault>
{"answer": "4.0"}
{"answer": "4.0"}

我已经找到了一个答案或者没有版本的端点不能工作的原因。如果我在API定义中的URL模式中添加了一些参数(例如/a/{param},或/a?param={param}),API描述(xml文件)中的资源将使用属性URL模板=/a/{param}生成,在这种情况下,如果我从URL模式中删除{param},并通过按钮插入参数,则该端点在没有版本的情况下无法工作“添加参数”-没有版本的端点(默认值)工作正常。在这种情况下,API描述资源是使用属性url映射生成的,而不是使用url模板生成的(就像在ESB中一样,我认为资源中有两种类型的url)

所以,当我习惯于使用没有版本(默认)的端点时,我必须拒绝在URL模式中显式使用路径和查询参数

也许有一些解决办法


还有一个小问题。如果我习惯于通过在GUI中按add parameter按钮来添加“path”参数,我就不能这样做,因为没有“path”选项“在选择列表中,只有查询、标题和formData。

我找到了一个答案或没有版本的端点无法工作的原因。如果我在API定义中的URL模式中添加了一些参数(例如/a/{param},或/a?param={param},API描述(xml文件)中的资源)使用属性url模板生成,在这种情况下,如果没有版本,该端点将无法工作。如果我从url模式中删除{param},并通过“添加参数”按钮插入参数,则无版本的端点(默认值)工作正常。在这种情况下,API描述资源是使用属性url映射生成的,而不是使用url模板生成的(就像在ESB中一样,我认为资源中有两种类型的url)

所以,当我习惯于使用没有版本(默认)的端点时,我必须拒绝在URL模式中显式使用路径和查询参数

也许有一些解决办法


还有一个小问题。如果我习惯于在GUI中按“添加参数”按钮来添加“路径”参数,我就不能这样做,因为没有“路径”选项在选择列表中,只有查询、标题和表单数据。

此问题将在下一版本中解决,您可以使用以下解决方法来解决此问题

用于现有API

  • 编辑默认api的synapse配置文件,该文件位于/repository/deployment/server/synapse configs/default/api文件夹中
例: API-admin--CountryTest_v1.0.0.xml版本的文件名, 默认API的文件名-admin--CountryTest.xml

  • 替换资源标记内的uri模板值并保存文件

    • 当前值-uri模板=“$util.escapeXml($resource.getUriTemplate())”
    • 新值-uri模板=“/*”
用于新的API

您可以更改默认的_api_template.xml文件,以便为新api应用修复程序

  • 编辑/repository/resources/api_templates文件夹中的默认_api_template.xml以替换新值中的资源标记
当前值-

<resource xmlns="http://ws.apache.org/ns/synapse"
    #if($resource.getUriTemplate().contains("{") ||
       ($resource.getUriTemplate().contains("*") && !$resource.getUriTemplate().endsWith("/*")))
        uri-template="$util.escapeXml($resource.getUriTemplate())"
    #else
       url-mapping="$resource.getUriTemplate()"
    #end
        methods="$resource.getMethodsAsString()" faultSequence="fault">

新价值-

<resource methods="POST PATCH GET DELETE HEAD PUT OPTIONS"
         uri-template="/*"
         faultSequence="fault">

此问题将在下一版本中修复,您可以使用以下解决方法来解决此问题

用于现有API

  • 编辑默认api的synapse配置文件,该文件位于/repository/deployment/server/synapse configs/default/api文件夹中
例: API-admin--CountryTest_v1.0.0.xml版本的文件名, 默认API的文件名-admin--CountryTest.xml

  • 替换资源标记内的uri模板值并保存文件