首页 / 浏览问题 / 云GIS / 问题详情
利用python编码实现从工作空间发布伟服务的过程出现错误
10EXP 2023年10月18日
import requests
import json

host = '192.168.221.111'

def getToken(session, user, password):
    url = f"http://{host}:8090/iserver/services/security/tokens.rjson"
    data = {
        "userName": user,
        "password": password,
        "clientType": "NONE",
        "expiration": 10
    }
    headers = {"Content-Type": "application/json"}
    response = session.post(url, data=json.dumps(data), headers=headers)
    if response.status_code == 200:
        token = response.text
        # 登录成功后的操作
        print("登录成功")
        # 可以在这里添加其他操作
    else:
        print("登录失败")
        # 可以在这里处理登录失败的情况
    return token


def getWorkSpace(session, token, host):
    url = 'http://' + host + ':8090/iserver/manager/workspaces.rjson?token=' + token
    data = {"workspaceConnectionInfo":"/home/fanqy/tmp5.smwu","servicesTypes":["RESTTRANSPORTATIONANALYST"],"isMultiInstance":'false',"instanceCount":"","transportationAnalystSetting":{"weightFieldInfos":[{"backWeightField":"SmLength","forwardWeightField":"SmLength","name":"SmLength"}],"workspaceConnectString":"/home/fanqy/tmp5.smwu","datasourceName":"my_datasource","datasetName":"result","nodeIDField":"SmNodeID","edgeIDField":"SmEdgeID","fromNodeIDField":"SmFNode","toNodeIDField":"SmTNode","initFacilityAnalyst":'false',"directionField":""}}
    #发送post请求
    headers = {}
    response = session.post(url, data=data, headers=headers)
    if response.status_code == 200:
        created_services = response.json()
        print(created_services)
        # for service_info in created_services:
        #     print(f"Service Type: {service_info['serviceType']}")
        #     print(f"Service Address: {service_info['serviceAddress']}")
    else:
        print(f"Failed to create services. Status code: {response.text}")

代码如上面所示,报错信息为:登录成功
P4Uq7AAbNao7n55CELiuWOHa24NGVKacDFg2goZfNdC5Nh4-GlURKG1FFm5yK04am4_wiwY_FQMwudVmz8uMLw..
Failed to create services. Status code: {
    "succeed": false,
    "error": {
        "code": 500,
        "errorMsg": "class java.lang.String cannot be cast to class com.supermap.services.rest.management.PublishServiceParameter (java.lang.String is in module java.base of loader 'bootstrap'; com.supermap.services.rest.management.PublishServiceParameter is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @9fec931)"
    }
}

Process finished with exit code 0

请问应该怎么解决?

1个回答

您好,您这边使用的iserver具体是什么版本呢(例如:11.1.1),您测试一下以下情况:

1.直接在iserver页面发布直接该工作空间是否正常

2.如果iserver页面发布该工作空间也出现500报错,请尝试发布其他工作空间例如示例数据工作空间发布是否正常
3,088EXP 2023年10月18日
直接在iserver页面发布工作空间是可以的,用的版本是supermap-iserver-11.1.0-linux-x64
您好,排查方向建议如下:

1.打印出python组件发送的post请求,

2.在iserver页面发布工作空间时查看网络请求,

3.对比二者请求报文差别

4.修正python组件请求的格式
经过检查,python组件请求的格式是正确的,但是仍然报错,是怎么回事呢
根据报错信息来看,getWorkspace方法中data类型为string,而PublishServiceParameter 所需参数类型为json,需要转为json再发送请求
...