dreamhead / moco

Easy Setup Stub Server
MIT License
4.36k stars 1.08k forks source link

如何使用Template从xml请求体中获取元素,填充到响应体中? #272

Open waitshang opened 4 years ago

waitshang commented 4 years ago

如何使用Template从xml请求体中获取元素,填充到响应体中?

dreamhead commented 4 years ago

No, currently it is not supported.

Could you please provide your case?

waitshang commented 4 years ago

No, currently it is not supported.

Could you please provide your case?

image 返回如下:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <foo>snlkoyltt8vc1cc6yg4llb9aqbjh4jbt</foo>
    <msg>SUCCESS</msg>
    <timeStamp>2020-03-12 16:00:00</timeStamp>
</xml>

我需要拿到foo节点的值。

wodeta33 commented 4 years ago

No, currently it is not supported. Could you please provide your case?

image 返回如下:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <foo>snlkoyltt8vc1cc6yg4llb9aqbjh4jbt</foo>
    <msg>SUCCESS</msg>
    <timeStamp>2020-03-12 16:00:00</timeStamp>
</xml>

我需要拿到foo节点的值。

+1

palmlan commented 4 years ago

看文档可以用xpath来提取request中的xml节点的文本,但实际报错,提示如下:

com.github.dreamhead.moco.MocoException: freemarker.core.NonStringOrTemplateOutputException: For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), or "template output" , but this has evaluated to a sequence (String[] wrapped into f.t.DefaultArrayAdapter$ObjectArrayAdapter):
==> foo  [in template "template" at line 1, column 3]

----
FTL stack trace ("~" means nesting-related):
        - Failed at: ${foo}  [in template "template" at line 1, column 1]
----
        at com.github.dreamhead.moco.resource.reader.TemplateResourceReader.readFor(TemplateResourceReader.java:92)
        at com.github.dreamhead.moco.resource.Resource.readFor(Resource.java:34)
        at com.github.dreamhead.moco.handler.ContentHandler.responseContent(ContentHandler.java:25)

测试用配置sample.json:

        {
                "description": "检查request的xml节点值,并利用自定义变量和xpath在response中返回这个值",
                "request": {
                      "xpaths" :
                        {
                                "/request/parameters/id[1]/text()" : "1"
                        }
                },
                "response": {
                        "headers" :
                        {
                                "Content-Type" : "text/xml"
                        },
                        "text": {
                                 "template": {
                                        "with" : "${foo}",
                                        "vars" : {
                                                "foo" : {
                                                        "xpaths" : "/request/parameters/id/text()"
                                                }
                                        }
                                }
                        }
                }
        }

]

服务启动命令行:

 java -jar moco-runner-1.1.0-standalone.jar http -p 8080 -c sample.json

请求命令行:

curl -H "Content-Type: text/xml" -X POST localhost:8080/xml -d @- < sample.xml

sample.xml:

<request><parameters><id>1</id></parameters></request>
iamxiaoguizi commented 4 years ago

@palmlan 文档里只说了可以用xpath,并没有说template里面可以支持xpath哦,我想这就是你的报错的原因吧。期望大佬解决这个问题。

iamxiaoguizi commented 4 years ago

No, currently it is not supported. Could you please provide your case?

image 返回如下:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <foo>snlkoyltt8vc1cc6yg4llb9aqbjh4jbt</foo>
    <msg>SUCCESS</msg>
    <timeStamp>2020-03-12 16:00:00</timeStamp>
</xml>

我需要拿到foo节点的值。

+2

@dreamhead

yzq449008766 commented 4 years ago

@dreamhead 大神您好,请问这个问题该如何解决?现在我急需您的帮助。。。