Json 如何发布对象及其子对象?grails3restful-security-POST保存新对象

Json 如何发布对象及其子对象?grails3restful-security-POST保存新对象,json,grails,spring-security,http-post,restful-architecture,Json,Grails,Spring Security,Http Post,Restful Architecture,我正在尝试执行一个简单的POST来保存一个新对象 我想发布一个具有以下属性的问题: String name static hasOne = [questionType: QuestionType] static belongsTo = [questionType: QuestionType] static hasMany = [propositions: Proposition] static constraints = { } static mapping = { proposi

我正在尝试执行一个简单的
POST
来保存一个新对象 我想发布一个具有以下属性的问题:

String name
static hasOne = [questionType: QuestionType]
static belongsTo = [questionType: QuestionType]
static hasMany = [propositions: Proposition]

static constraints = {
}

static mapping = {
    propositions cascade: 'all-delete-orphan'
}
每个
问题
都有一个抽象的
问题类型
由以下对象之一扩展:

  • Mcq
    (选择题)
  • SourceCode
  • 匹配
对于我的
Mcq
,我具有以下属性:

String name
static hasOne = [questionType: QuestionType]
static belongsTo = [questionType: QuestionType]
static hasMany = [propositions: Proposition]

static constraints = {
}

static mapping = {
    propositions cascade: 'all-delete-orphan'
}
因此,我的
Mcq
拥有0到多个
命题

命题
由以下部分组成:

String body
Boolean isCorrect
String reason 
我正在使用邮递员检查我的API。我现在没有任何用户界面

我的
问题
Qcm
命题
对象有一个控制器和一个服务来管理它们

我的
问题服务

class QuestionService {
    def save(Question question) {
        if (question == null) {
            transactionStatus.setRollbackOnly()
            render status: NOT_FOUND
            return
        }
        if (question.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond question.errors, view:'create'
            return
        }
        question.save flush:true
    }

    def update(Question question) {
        if (question == null) {
            transactionStatus.setRollbackOnly()
            render status: NOT_FOUND
            return
        }
        if (question.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond question.errors, view:'edit'
            return
        }
        question.save flush:true
    }

    def delete(Question question) {
        if (question == null) {
            transactionStatus.setRollbackOnly()
            render status: NOT_FOUND
            return
        }
        question.delete flush: true
    }
}
我的
问题控制器

@Secured('ROLE_ADMIN')
@Transactional(readOnly = true)
class QuestionController {

    //spring bean injected for the Controller to access the Service
    def questionService

    static responseFormats = ['json', 'xml']
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    //views
    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Question.list(params), model:[questionCount: Question.count()]
    }

    def show(Question question) {
        respond question
    }

    //actions
    def save(Question question){
        try { questionService.save question }
        catch (e){ respond status: PRECONDITION_FAILED }
        respond question, [status: CREATED, view:"show"]
    }

    def update(Question question) {
        try { questionService.update question }
        catch (e) { respond status: PRECONDITION_FAILED }
        respond question, [status: OK, view:"show"]
    }

    def delete(Question question) {
        try { questionService.delete question }
        catch (e){ respond status: PRECONDITION_FAILED }
        respond status: OK, view:"index"
    }
}
我可以用我的索引获取每个
命题
Mcq
问题
,并显示操作。 我不允许我的用户发布新的
提案
/
Mcq
。如果他们想这样做,他们必须更新完整的问题本身。因此,只有我的
问题
可以修改(和修改其他问题)、其他问题或只读问题

我的问题是我不能
发布
这个:

{
  "name": "name1",
  "questionType": {
    "propositions": [
        {
            "body": "body proposition 1",
            "isCorrect": true,
            "reason": "reason proposition 1"
        },
        {
            "body": "body proposition 2",
            "isCorrect": false,
            "reason": "reason proposition 2"
        }
    ]
  }
}
它总是给我一个惊喜

[
     null
]
但这是可行的:

{
  "name": "name1",
  "questionType": {
    "id": 1
  }
}

因此,在这里,我可以引用数据库中已经存在的
问题类型
,但我不知道如何级联创建我的
问题
问题类型
Qcm
命题
你说的Mcq扩展问题类型,但是Mcq属于问题类型。您需要显示您对持久化感兴趣的所有域类。这里有太多的错误,我是新来的。所以我真的不知道如何管理所有这些。这是我第一次使用ORM。所以我应该只扩展我的类,而不使用belongsTo?我认为我的扩展必须通过一个belongsTo连接到它的母亲