问题描述
类PersonController扩展了RestfulController< Person> {
$ b $ staticResponseFormats = ['json','xml']
PersonController(){
super(Person)
}
}
然而,现在我想为此添加一个搜索选项。什么是Grails使之成为可能的方式?
我想添加以下内容:
def search(Map params){
println params
}
但是,这让Grails(2.3)崩溃(|编译期间错误致命错误org.apache.tools.ant.BuildException:编译失败(使用--stacktrace查看完整跟踪))。
那么添加这个的正确方法是什么?我正在寻找一些解决方案,可以使用 http:// localhost:8080 / foo / person / search?q = erik
这是我的UrlMappings:
静态映射= {
/ $ controller / $ $ {
约束{
//在这里应用约束
}
}
/ rest / persons(resources:'Person')
以上为:
def search(){
println params
}
这并不会导致编译错误,但我仍然遇到这个错误:
处理请求时发生TypeMismatchException:[GET] / declaratie-web / rest / medicaties / search - 参数:
q:erik
提供的类nl.Person的错误类型。预期:类java.lang.Long,得到类java.lang.String。 Stacktrace如下:
org.hibernate.TypeMismatchException:为类nl.Person提供了错误类型的id。预期:类java.lang.Long,得到类java.lang.String
我也发现无论我怎么打电话给控制器:
http:// localhost:8080 / foo / person / search?q = erik
http:// localhost:8080 / foo / person / search222?q = erik
http:// localhost:8080 / foo / person / search39839329?q = erik
所有失败都会出现上述错误,所以看起来我的方法被忽略了(可能是由于我的URL映射造成的)?
你真的不会因为这样做而成为RESTful。 q
应该只是索引操作的一个参数。您可以覆盖该方法以包含您的功能。
def index(整数最大值){
params.max =数学.min(max?:10,100)
def c = Person.createCriteria()
def results = c.list(params){
//使用params.q
}
回应结果,模型:[personCount:results.totalCount]
}
I'm having a very simple restful controller, which looks like this:
class PersonController extends RestfulController<Person> {
static responseFormats = ['json', 'xml']
PersonController() {
super(Person)
}
}
However, now I want to add a search option to this. What is the Grails way of making this possible?
I thought of adding the following:
def search(Map params) {
println params
}
But that makes Grails (2.3) crash (| Error Fatal error during compilation org.apache.tools.ant.BuildException: Compilation Failed (Use --stacktrace to see the full trace)).
So what is the right way of adding this? I'm looking for some solution which I can call using http://localhost:8080/foo/person/search?q=erik
This is my UrlMappings:
static mappings = {
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
"/rest/persons"(resources:'Person')
I've changed the above to:
def search() {
println params
}
And that doesn't give the compilation error anymore, but I still get this error:
TypeMismatchException occurred when processing request: [GET] /declaratie-web/rest/medicaties/search - parameters:
q: erik
Provided id of the wrong type for class nl.Person. Expected: class java.lang.Long, got class java.lang.String. Stacktrace follows:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class nl.Person. Expected: class java.lang.Long, got class java.lang.String
I also found out that it doesn't matter how I call the controller:
http://localhost:8080/foo/person/search?q=erik
http://localhost:8080/foo/person/search222?q=erik
http://localhost:8080/foo/person/search39839329?q=erik
All fails with the above error, so it seems my method is ignored (maybe caused by my URLmapping?)
You really aren't being RESTful by doing that. q
should just be a parameter for the index action. You can override that method to include your functionality.
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
def c = Person.createCriteria()
def results = c.list(params) {
//Your criteria here with params.q
}
respond results, model:[personCount: results.totalCount]
}
这篇关于为Grails restfulcontroller添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!