I'm having trouble understanding how Grails Restful controllers work. I am trying to make a request by mail (see below) to the attached resource. I'm not sure I understand what I need to change to make this work, since it seems that GET requests are building a Bid association with its parent Item element, but when I try to execute POST, I warn that the element cannot be empty.
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 Mapping
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
Mail 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 }
Answer
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"}]}
source share