John Little
John Little

Reputation: 12338

Grails 3.3.8 - how to get session from a service

Ok, I have a multimodule project, with the services in a plugin, and multiple web "applicaions" which use it.

The user and player services have a login and register method which needs to "login" the player by updating the session.

This post:Grails get Session and Management in Service class

Has some solutions, but none actually seem to work. Webutils is not available in any of these locations

import org.codehaus.groovy.grails.web.util.WebUtils
import org.grails.web.util.WebUtil
import org.grails.web.util.WebUtils

nor does this work:

def session = getSession(true)

Any suggestions?

This is from build.gradle of the plugin:

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.grails.plugins:events"
    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.1.5.Final"
    compile "org.grails.plugins:scaffolding"
    console "org.grails:grails-console"
    profile "org.grails.profiles:plugin"
    provided "org.grails:grails-plugin-services"
    provided "org.grails:grails-plugin-domain-class"
    runtime "org.glassfish.web:el-impl:2.1.2-b03"
    runtime "com.h2database:h2"
    runtime "org.apache.tomcat:tomcat-jdbc"
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-plugin-testing"
}

Upvotes: 1

Views: 579

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27200

Any suggestions?

Yes.

You can do this:

// grails-app/services/somepackage/SomeService.groovy
package somepackage

import grails.web.api.ServletAttributes

class SomeService implements ServletAttributes {

    void someMethod() {
        // you can access the session property directly from here
        println session

        // ...
    }
}

Upvotes: 1

Related Questions