sezer.yilmaz
2014-02-21 23:07:08 UTC
Hi everyone,
since a couple of days I am evaluating some frameworks for building a
REST-Backend and I really like Grails. Now I've got the following Problem
for which I couldn't find a solution anywhere.
class UrlMappings {
...
"/api/v1/authors"(resources:"Author") {
"/books"(resources:"Book")
}
...
}
@Resource(formats=['json', 'xml'])
class Author {
String name
static hasMany = [books: Book]
static constraints = {
}
}
@Resource(formats=['json', 'xml'])
class Book {
String title;
String language;
static belongsTo = [author: Author]
static constraints = {
}
}
class BootStrap {
def init = { servletContext ->
def author1 = new Author(name: "Goethe").save(failOnError: true)
def author2 = new Author(name: "Kafka").save(failOnError: true)
def book1 = new Book(title: "Bla Bla Book 1", language: "German",
author: author1).save(failOnError: true)
def book2 = new Book(title: "Blah Book 2", language: "German",
author: author1).save(failOnError: true)
def book3 = new Book(title: "Blaaah Book 3", language: "German",
author: author1).save(failOnError: true)
}
def destroy = {
}
}
This is all I have, really simple. Thanks to the scaffolding etc. i get the
following result when I call
http://localhost:8080/GrailsTest/api/v1/authors/1/books ->
[
{
class: "grailstest.Book",
id: 1,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Bla Bla Book 1"
},
{
class: "grailstest.Book",
id: 2,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blah Book 2"
},
{
class: "grailstest.Book",
id: 3,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blaaah Book 3"
}
]
Really really cool that this works with just a couple of lines of code.
BUT HERE'S MY PROBLEM:
When I call the books of the second author which really has no books I get
again all the books in the database.
http://localhost:8080/GrailsTest/api/v1/authors/2/books ->
[
{
class: "grailstest.Book",
id: 1,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Bla Bla Book 1"
},
{
class: "grailstest.Book",
id: 2,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blah Book 2"
},
{
class: "grailstest.Book",
id: 3,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blaaah Book 3"
}
]
What am I doing wrong ? Would be nice if someone could point me to the right
direction.
Thanks
--
View this message in context: http://grails.1312388.n4.nabble.com/Nested-RESTful-Ressources-tp4654317.html
Sent from the Grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
since a couple of days I am evaluating some frameworks for building a
REST-Backend and I really like Grails. Now I've got the following Problem
for which I couldn't find a solution anywhere.
class UrlMappings {
...
"/api/v1/authors"(resources:"Author") {
"/books"(resources:"Book")
}
...
}
@Resource(formats=['json', 'xml'])
class Author {
String name
static hasMany = [books: Book]
static constraints = {
}
}
@Resource(formats=['json', 'xml'])
class Book {
String title;
String language;
static belongsTo = [author: Author]
static constraints = {
}
}
class BootStrap {
def init = { servletContext ->
def author1 = new Author(name: "Goethe").save(failOnError: true)
def author2 = new Author(name: "Kafka").save(failOnError: true)
def book1 = new Book(title: "Bla Bla Book 1", language: "German",
author: author1).save(failOnError: true)
def book2 = new Book(title: "Blah Book 2", language: "German",
author: author1).save(failOnError: true)
def book3 = new Book(title: "Blaaah Book 3", language: "German",
author: author1).save(failOnError: true)
}
def destroy = {
}
}
This is all I have, really simple. Thanks to the scaffolding etc. i get the
following result when I call
http://localhost:8080/GrailsTest/api/v1/authors/1/books ->
[
{
class: "grailstest.Book",
id: 1,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Bla Bla Book 1"
},
{
class: "grailstest.Book",
id: 2,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blah Book 2"
},
{
class: "grailstest.Book",
id: 3,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blaaah Book 3"
}
]
Really really cool that this works with just a couple of lines of code.
BUT HERE'S MY PROBLEM:
When I call the books of the second author which really has no books I get
again all the books in the database.
http://localhost:8080/GrailsTest/api/v1/authors/2/books ->
[
{
class: "grailstest.Book",
id: 1,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Bla Bla Book 1"
},
{
class: "grailstest.Book",
id: 2,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blah Book 2"
},
{
class: "grailstest.Book",
id: 3,
author: {
class: "Author",
id: 1
},
language: "German",
title: "Blaaah Book 3"
}
]
What am I doing wrong ? Would be nice if someone could point me to the right
direction.
Thanks
--
View this message in context: http://grails.1312388.n4.nabble.com/Nested-RESTful-Ressources-tp4654317.html
Sent from the Grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email