【问题标题】:Method on class [] was used outside of a Grails application controller类 [] 上的方法在 Grails 应用程序控制器之外使用
【发布时间】:2015-05-22 04:14:25
【问题描述】:

具有以下 grails 配置: 数据源。

environments {
development {
    dataSource {
        dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
        url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }

    datasource_staging_oracle {
        dbCreate = "none"
        url = "jdbc:oracle:thin:@//myoraclehost:1521/DBNAME"
        driverClassName = "oracle.jdbc.OracleDriver"
        username = "username"
        password = "password"
    }
}

领域类:

import org.springframework.integration.Message

class SpringMessage {

    static mapping = {
        datasource 'staging_oracle'
        message type: 'blob', column: 'message_bytes'
        createdDate type: Date, column: 'created_date'
    }
    static constraints = {
    }

    String messageId
    Message<?> message
    Date createdDate
}

在控制器内部,使用以下方法获取记录:

SpringMessage springMessage = SpringMessage.findByMessageId('messsage_id_value')

以上行失败并出现以下错误: 类 [com.foo.bar.SpringMessage] 上的方法在 Grails 应用程序之外使用。如果在使用模拟 API 或引导 Grails 的测试上下文中正确运行。

如何解决这个问题?谷歌搜索显示 grails“测试”相关的帖子。但这不是测试代码。上面的 findBy 方法是从 grails 控制器调用的。 我在 grails 2.3.3 上,遗憾的是目前无法升级到最新的 grails。

更新
控制器代码:

class FooController {
    def index() {
        foo2()
    }
    private def foo2() {
        SpringMessage springMessage = SpringMessage.findByMessageId('my_message_id') //This line blowsup
        if ( springMessage) {
            println springMessage.createdDate
        } else {
            println "not found"
        }
    }
}

我使用http://localhost:8080/myapp/foo/index 访问控制器
更新
在我原来的问题中,Blob 列声明不正确。正确版本如下:

class SpringMessage {

    static mapping = {
        datasource 'staging_oracle'
        message type: 'blob', column: 'message_bytes'
        createdDate type: Date, column: 'created_date'
    }
    static constraints = {
    }

    String messageId
    Blob message
    Date createdDate
}

【问题讨论】:

  • 你能提供控制器实现吗?
  • @saw303 用控制器 sn-p 更新了帖子
  • 如何运行 Grails?
  • 它来自 Intellijj。使用“run-app”参数运行 grails。
  • 您可以从命令行尝试并添加完整的 Stacktrace 吗?从命令行运行 grails run-app

标签: grails grails-orm grails-2.0 grails-domain-class grails-2.3


【解决方案1】:

问题是因为 DataSource.groovy 中的拼写错误: 数据源字应具有大写的“S”。哎呀。 Grails 应该对此提出警告。

environments {
  development {
    dataSource {
        dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
        url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }

    dataSource_staging_oracle {
        dbCreate = "none"
        url = "jdbc:oracle:thin:@//myoraclehost:1521/DBNAME"
        driverClassName = "oracle.jdbc.OracleDriver"
        username = "username"
        password = "password"
    }
}

【讨论】:

    【解决方案2】:

    您的域类SpringMessage 使用org.springframework.integration.Message 作为域属性message。有错误试图表明org.springframework.integration.Message 不是Grails 域对象,不能映射到数据库。

    您需要引入一个域对象来保存您的org.springframework.integration.Message 实例的相关数据。可能您正在尝试存储 Spring Integration Message 的有效负载。

    class SpringMessage {
    
      static mapping = {
        datasource 'staging_oracle'
        message type: 'blob', column: 'message_bytes'
        createdDate type: Date, column: 'created_date'
      }
      static constraints = {
      }
    
      String messageId
      String message // use a String instead of the message instance
      Date createdDate
    }
    

    然后将payload设置为SpringMessage

    new SpringMessage(messageId: 'ID', message: message.payload, createdDate: new Date()).save()
    

    希望这会有所帮助。

    【讨论】:

    • 当我将message 属性类型更改为字符串时仍然出现同样的错误
    【解决方案3】:

    我刚遇到这个问题。

    在我的 DataSource.groovy 中,我将辅助数据源指定为 dataSource_mysql,而在我的域类映射块中,我也使用了它。结果在域类中我需要将数据源引用为 mysql 而没有 dataSource_

    进行此更改后,我就可以从控制器调用域方法而不会引发此错误。

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多