发布期间指定内容类型时,Grails无法解析请求 [英] Grails fails to parse request when content type is specified during post

查看:85
本文介绍了发布期间指定内容类型时,Grails无法解析请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用wget我在正文中使用xml将两个帖子发布到相同的url.第一部作品.第二个具有指定的内容类型,它不起作用.为什么会这样?即使指定了内容类型,我怎么也无法解析请求?

Using wget I make two posts to the same url with xml in the body. The first works. The second has the content type specified and it does not work. Why is this and how do I get grails to parse the request even when the content type is specified?

第一个wget:

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

Grails日志(请注意解析的参数"):

Grails logs (notice the "parsed params"):

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日志(请注意,没有解析的参数,并且主体具有所有xml):

Grails logs (notice no parsed params, and the body has all the 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

Grails version 2.3.5

更新

网址映射:

class UrlMappings {

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

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

推荐答案

使用内容类型指定的内容将作为请求正文的一部分接收,而不是解析的参数.

With content type specified content will be received as part of request body instead of parsed params.

实际上,可以将其作为request.XML进行访问.通过评论,我们发现使用bindData的旧方法.使用Grails 2.3.*,只要主体具有有效的id:

Body can be accessed as request.XML in action. Going through the comments, we found out the usage of a older way to bindData. With Grails 2.3.*, the request payload can be directly bound to a domain object provided the body has a valid id as :

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

如果请求正文中没有有效的ID,则域对象在操作内将为null.

If a valid id is not present in request body then domain object will be null inside the action.

@ 数据绑定.

这篇关于发布期间指定内容类型时,Grails无法解析请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆