在Grails中发布嵌套资源的问题 [英] Issues posting nested resource in Grails

查看:105
本文介绍了在Grails中发布嵌套资源的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Grails Restful控制器如何工作的问题。我正在尝试向嵌套资源发出发布请求(请参见下文)。我不确定我是否理解我需要更改以完成此项工作,因为GET请求似乎会与其父资源Item建立投标关联,但是当我尝试发布POST时,我收到警告,该项目不能为空。

Item.groovy





pre> class Item {
static hasMany = [bids:Bid]
}

Bid.groovy

  class出价{
整数拥有者ID
双重金额

static belongsTo = [item:Item]

static constraints = {
ownerId可空:false
可以为null:false
} b



BidController.groovy

 类BidController扩展了RestfulController< Bid> {
static responseFormats = ['json','xml']
BidController(){
super(Bid)
}
@Override
def getObjectToBind (){
request.parameterMap.put('itemId',params.itemId)
返回请求
}
}

ItemController.groovy

 类ItemController扩展了RestfulController< Item> {
static responseFormats = ['json','xml']
ItemController(){
super(Item)
}
}

UrlMappings.groovy

  class UrlMappings {

static mappings = {
/ items(resources:item){
/ bids(resources:bid)
}




$ b

URL映射

 控制器:项目
| GET | / items |操作:索引
| GET | / items / create |行动:创建
| POST | / items |行动:保存
| GET | / items / $ {id} |行动:显示
| GET | / items / $ {id} / edit |操作:编辑
| PUT | / items / $ {id} |行动:更新
| PATCH | / items / $ {id} |行动:补丁
|删除| / items / $ {id} |操作:删除
控制器:出价
| GET | / items / $ {itemId} / bids |操作:索引
| GET | / items / $ {itemId} / bids / create |行动:创建
| POST | / items / $ {itemId} / bids |行动:保存
| GET | / items / $ {itemId} / bids / $ {id} |行动:显示
| GET | / items / $ {itemId} / bids / $ {id} / edit |操作:编辑
| PUT | / items / $ {itemId} / bids / $ {id} |行动:更新
| PATCH | / items / $ {itemId} / bids / $ {id} |行动:补丁
|删除| / items / $ {itemId} / bids / $ {id} |行动:删除

发送请求

  POST / AuctionService / items / 1 / bids HTTP / 1.1 
Content-Type:application / json
Host:localhost:8080
Connection:close
内容长度:34

{
ownerId:1,
金额:3.00
}

响应

  HTTP / 1.1 422无法处理的实体
Server:Apache-Coyote / 1.1
Content-Type:application / json; charset = UTF-8
Transfer-Encoding:chunked
日期:2014年7月25日,星期五17:44 :03 GMT
连接:关闭

{错误:[{object:auctionservice.Bid,field:item,rejected-value:null ,message:[class auctionservice.Bid]的属性[item]不能为null}]}


解决方案

我认为你可以通过重写createResource()方法来实现你想要的。

c $ c> @Override
保护投标createResou rce(){

Bid bid = super.createResource();
bid.item = Item.get(params.itemId)
返回出价;
}

使用嵌套网址时,其他默认控制器操作可能无法按预期工作。
如果您想确保只返回属于URL中的项目的出价,您可能还想覆盖queryForResource和index

  @Override 
保留queryForResource(Serializable id){
def itemId = params.itemId

Bid.where {
id == id& amp; amp; ;&安培; item.id == itemId
。.find()

}

def index(整数最大值){
params.max = Math.min (max?:10,100)
def itemId = params.itemId
响应Bid.where {
item.id == itemId
} .list(params)
}


I'm having issues understanding how Grails Restful controllers work. I'm attempting to make a post request (see below) to a nested resource. I'm not sure I understand what I need to change to make this work, as it seems that GET requests build the Bid's association with it's parent resource Item, but when I attempt to POST I am warned that the Item cannot be blank.

Any help is appreciated!

Item.groovy

class Item {
    static hasMany = [bids:Bid]
}

Bid.groovy

class Bid {
    Integer ownerId
    Double amount

    static belongsTo = [item:Item]

    static constraints = {
        ownerId nullable: false
        amount nullable: false
    }
}

BidController.groovy

class BidController extends RestfulController<Bid> {
    static responseFormats = ['json', 'xml']
    BidController() {
        super(Bid)
    }
    @Override
    def getObjectToBind() {
        request.parameterMap.put('itemId', params.itemId)
        return request
    }
}

ItemController.groovy

class ItemController extends RestfulController<Item> {
    static responseFormats = ['json', 'xml']
    ItemController() {
        super(Item)
    }
}

UrlMappings.groovy

class UrlMappings {

    static mappings = {
        "/items"(resources:"item") {
            "/bids"(resources: "bid")
        }
    }
}

URL Mappings

Controller: item
 |   GET    | /items                                                    | Action: index            
 |   GET    | /items/create                                             | Action: create           
 |   POST   | /items                                                    | Action: save             
 |   GET    | /items/${id}                                              | Action: show             
 |   GET    | /items/${id}/edit                                         | Action: edit             
 |   PUT    | /items/${id}                                              | Action: update           
 |  PATCH   | /items/${id}                                              | Action: patch            
 |  DELETE  | /items/${id}                                              | Action: delete    
Controller: bid
 |   GET    | /items/${itemId}/bids                                     | Action: index            
 |   GET    | /items/${itemId}/bids/create                              | Action: create           
 |   POST   | /items/${itemId}/bids                                     | Action: save             
 |   GET    | /items/${itemId}/bids/${id}                               | Action: show             
 |   GET    | /items/${itemId}/bids/${id}/edit                          | Action: edit             
 |   PUT    | /items/${itemId}/bids/${id}                               | Action: update           
 |  PATCH   | /items/${itemId}/bids/${id}                               | Action: patch            
 |  DELETE  | /items/${itemId}/bids/${id}                               | Action: delete                    

Post Request

POST /AuctionService/items/1/bids HTTP/1.1
Content-Type: application/json
Host: localhost:8080
Connection: close
Content-Length: 34

{
    "ownerId": 1,
    "amount": 3.00
}

Response

HTTP/1.1 422 Unprocessable Entity
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 25 Jul 2014 17:44:03 GMT
Connection: close

{"errors":[{"object":"auctionservice.Bid","field":"item","rejected-value":null,"message":"Property [item] of class [class auctionservice.Bid] cannot be null"}]}

解决方案

I think you can accomplish what you want by overriding the createResource() method.

@Override
protected Bid createResource() {

    Bid bid=super.createResource();
    bid.item=Item.get(params.itemId)
    return bid;
}

The other default controller actions will probably not work as expected when using nested URLs. You may also want to override queryForResource and index if you want to ensure that you only return Bids that belong to the item in the URL

@Override
protected Stay queryForResource(Serializable id) {
    def itemId=params.itemId

    Bid.where {
        id==id &&  item.id == itemId
    }.find()

}

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    def itemId=params.itemId
    respond Bid.where {
        item.id==itemId
    }.list(params)
}

这篇关于在Grails中发布嵌套资源的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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