在发布期间指定内容类型时,Grails无法解析请求

时间:2014-02-28 02:48:37

标签: grails

使用wget我在正文中使用xml为同一个网址发了两个帖子。第一部作品。第二个指定了内容类型,但它不起作用。为什么这样,即使指定了内容类型,如何使用grails来解析请求?

首先wget:

wget http://localhost:8080/myApp/myMoeView/save --post-file=xmltest.xml

Grails日志(请注意“已解析的参数”):

2014-02-27 18:44:05,465 [http-bio-8080-exec-9] INFO  httplogger.DefaultHttpLogger  - << #3425 POST http://localhost:8080/myApp/mrMoeView/save
2014-02-27 18:44:05,465 [http-bio-8080-exec-9] INFO  httplogger.DefaultHttpLogger  - << #3425 headers [Cookie: JSESSIONID=D5B2399D6FFB800130E826DCD7DB0C37]
2014-02-27 18:44:05,465 [http-bio-8080-exec-9] INFO  httplogger.DefaultHttpLogger  - << #3425 body: ''
2014-02-27 18:44:05,491 [http-bio-8080-exec-9] INFO  httplogger.DefaultHttpLogger  - << #3425 dispatched to mrMoeView/save with parsed params ['<?xml version':'{"1.0" encoding="UTF-8" standalone="yes"?><mrMoeView><absoluteTolerance>1.004</absoluteTolerance><endTime>0</endTime><id>4187</id><lastModified>2014-01-07 00:00:00.000 PST</lastModified><modelRealizationId>1193</modelRealizationId><mrMoeId>4187</mrMoeId><mrMoeName>Default MOE from Model 2140</mrMoeName><relativeTolerance>1e-4</relativeTolerance><startTime>0</startTime></mrMoeView>

第二个wget:

wget http://localhost:8080/myApp/mrMoeView/save --post-file=xmltest.xml --header="Content-Type:application/xml"

Grails日志(注意没有解析的params,并且正文包含所有xml):

2014-02-27 18:46:27,291 [http-bio-8080-exec-5] INFO  httplogger.DefaultHttpLogger  - << #3427 POST http://localhost:8080/processdb/mrMoeView/save
2014-02-27 18:46:27,291 [http-bio-8080-exec-5] INFO  httplogger.DefaultHttpLogger  - << #3427 headers [Cookie: JSESSIONID=B1FAAB54422AC7F1E243D4CE68C72B77]
2014-02-27 18:46:27,291 [http-bio-8080-exec-5] INFO  httplogger.DefaultHttpLogger  - << #3427 body: '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><mrMoeView><absoluteTolerance>1.004</absoluteTolerance><endTime>0</endTime><id>4187</id><lastModified>2014-01-07 00:00:00.000 PST</lastModified><modelRealizationId>1193</modelRealizationId><mrMoeId>4187</mrMoeId><mrMoeName>Default MOE from Model 2140</mrMoeName><relativeTolerance>1e-4</relativeTolerance><startTime>0</startTime></mrMoeView>'
2014-02-27 18:46:27,320 [http-bio-8080-exec-5] INFO  httplogger.DefaultHttpLogger  - << #3427 dispatched to mrMoeView/save with parsed params [].

Grails版本2.3.5

更新

urlMapping中:

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?"(parseRequest:true){
        constraints {
            // apply constraints here
        }
    }

    "/"(view:"/index")
    "500"(view:'/error')
}
}

1 个答案:

答案 0 :(得分:1)

对于内容类型,指定的内容将作为请求正文的一部分而不是已解析的参数接收。

可以在request.XML操作中访问正文。通过评论,我们发现使用较旧的方式bindData。使用Grails 2.3。*,请求有效负载可以直接绑定到域对象,前提是正文具有有效id

def save(MyDomain abc){
    //use abc.name
}

如果请求正文中没有有效的id,那么域对象在操作中将为null。

@ Data binding