在 prolog grails XML 错误中不允许

Not allowed in prolog grails XML error

我在 Curl 命令上有以下内容:

curl -i -H "Content-Type: text/xml" -d '<sample:WIB xmlns:sample="www.sample.com"><sample:Customer><sample:Catalog_ID>01</sample:Catalog_ID><sample:Shop_ID>01</sample:Shop_ID></sample:Customer></sample:WIB>' http://localhost:8080/app/API/

我的 Grails 控制器是:

def index() {
    String xml = request.XML.toString();
    def workXml = new XmlSlurper().parseText(xml)
}

解析时出现错误 我也试过这个:

def workXml = new XmlSlurper().parseText(xml).declareNamespace(sample: 'sample')

我得到同样的错误:

这是堆栈跟踪:

Message: Content is not allowed in prolog.
Line | Method
->>   10 | index     in app.APIController$$EP3387M0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter  in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     49 | doFilter  in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
|     82 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread

您对 curl 使用了错误的选项 - -d 选项需要 name=value 对并将它们视为来自浏览器的表单 post。如果您想按原样发送命令行上给出的数据,您应该使用 --data-binary 代替。

顺便说一句,request.XML 已经被解析了——它是一个 GPathResult 和你从 XmlSlurper 得到的一样,所以没有必要尝试将它转换回一个字符串并且又吸了一口。