【发布时间】:2021-07-17 01:55:13
【问题描述】:
我目前正在将 Grails 4 用于 RESTfull 应用程序。 有以下控制器,一个自定义的会抛出 json 响应的控制器:
package nslbv_siu
import grails.rest.*
import grails.converters.*
class LoginController
{
static allowedMethods = [ login:'POST' ]
static responseFormats = [ 'json', 'xml' ]
static defaultAction = "login"
def login()
{
if( params.user == '' )
{
render "{ 'succes':false, 'error':'Ingrese su usuario.' }"
}
if( params.pass == '' )
{
render "{ 'success':false, 'error':'Ingrese su contraseña.' }"
}
def attempted_user = User.queryUserByUsernameAndActive( params.user, true )
if( attempted_user )
{
if ( attempted_user.pass == params.pass.md5() )
{
render "{ 'success':true, 'error':'', " +
"'user':'" + attempted_user.user + "', " +
"'mail':'" + attempted_user.dni + "', " +
"'apenom':'" + attempted_user.apenom + "', " +
"'profile_pic':'" + attempted_user.profile_pic + "' }"
}
else
{
render "{ 'success':false, 'error':'Usuario o Contraseña incorrectos' }"
}
}
else
{
render "{ 'success':false, 'error':'Usuario o Contraseña incorrectos' }"
}
}
User queryUserByUsernameAndActive(String username, Boolean active)
{
User.all.find
{
it.user == username && it.active == active
}
}
}
需要调用http://localhost:8080/login?user=myuser&pass=mypass 但是 throws 404 not found,不知道为什么!我必须使用另一个约定来发布我的路由吗?
我的映射:
package nslbv_siu
class UrlMappings {
static mappings =
{
delete "/$controller/$id(.$format)?"(action:"delete")
get "/$controller(.$format)?"(action:"index")
get "/$controller/$id(.$format)?"(action:"show")
post "/$controller(.$format)?"(action:"save")
put "/$controller/$id(.$format)?"(action:"update")
patch "/$controller/$id(.$format)?"(action:"patch")
"/"(controller: 'application', action:'index')
"500"(view: '/error')
"404"(view: '/notFound')
"/login"(controller: "LoginController", action: "login") -->does not seem to work
}
}
我不明白为什么它不起作用!,我做错了什么吗?
grails url-mappings-report 抛出以下内容
动态映射 | * |错误:404 |查看:/未找到 | | * |错误:500 |查看:/错误|
控制器:登录控制器 |发布 | /登录 |操作:登录 |
控制器:应用程序 | * | / |行动:索引|
【问题讨论】: