2

我有 1 个域类、1 个控制器、1 个 URL 映射(见下文)。我想发送请求,并接收 JSON 响应。

但目前我的请求已正确映射到对应的控制器方法,该方法已成功执行,然后我收到一条错误消息,提示 jsp-file 不可用。

如何解释 Grails,我不需要 jsp 文件:我想接收/解析 JSON 请求,并直接从我的控制器发送 JSON 响应?

class Brand {

    String name
    String description
    String logoImageURL

    static constraints = {
        name(blank: false)
    }
}

---------------------------
class UrlMappings {
     static mappings = {
        "/brands"(controller: "brand", parseRequest: true, action: "list")
     }
}

---------------------------
import grails.converters.JSON

class BrandController {

    def list = {
        return Brand.list() as JSON
    }

    def show = {
        return Brand.get(params.id) as JSON
    }

    def save = {
        def brand = new Brand(name: params.name, description: params.description, logoImageURL: params.logoURL)
        if (brand.save()) {
            render brand as JSON
        } else {
            render brand.errors
        }
    }
}

============== Error message ===============
Request URL: http://localhost:8080/ShoesShop/brands

message /ShoesShop/WEB-INF/grails-app/views/brand/list.jsp

description The requested resource (/ShoesShop/WEB-INF/grails-app/views/brand/list.jsp) is not available.
4

1 回答 1

3

如果您改为这样做,它应该可以工作:

def list = {
    render Brand.list() as JSON
}
于 2012-05-17T14:48:07.607 回答