urlMap抛出404

im9ewurl  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(229)

我目前正在使用grails4作为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但找不到404,不知道为什么!,我必须使用另一个约定来发布我的路线吗?
我的Map:

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报告抛出以下内容
动态Map||错误:404 |视图:/notfound | ||错误:500 |视图:/error|
控制器:logincontroller | post |/登录|操作:登录|
控制器:应用程序|*|/|操作:索引|

h4cxqtbf

h4cxqtbf1#

它是:

"/login"(controller: "login", action: "login")

而不是

"/login"(controller: "LoginController", action: "login")

相关问题