Leandro Gamarra
Leandro Gamarra

Reputation: 151

URL mapping throws 404 not found on request GRAILS

Im currently using Grails 4 for a RESTfull App. Have the following controller, a custom one that throws a json response:

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
        }
    }
}

Need to call http://localhost:8080/login?user=myuser&pass=mypass But throws 404 not found, dont know why!, must I use another convention to make my routes be published?

My mappings:

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
    }
}

I dont understand why it does not work!, am I doing something wrong?

grails url-mappings-report throws the following

Dynamic Mappings | * | ERROR: 404 | View: /notFound | | * | ERROR: 500 | View: /error |

Controller: LoginController | POST | /login | Action: login |

Controller: application | * | / | Action: index |

Upvotes: 0

Views: 141

Answers (1)

Leandro Gamarra
Leandro Gamarra

Reputation: 151

It was:

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

instead of

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

Upvotes: 1

Related Questions