【问题标题】:Grails No thread-bound request foundGrails 未找到线程绑定请求
【发布时间】:2017-04-06 09:03:57
【问题描述】:

我正在使用 Grails 服务闭包对另一个 Web 应用程序执行 async HTTP request。使用Grails 1.3.9,我在这里非常有限。代码在线程内运行。它是从计划的作业和几个函数调用中调用的。我得到的错误如下:

错误:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

代码:

    Thread.start {

        def http = new HTTPBuilder(grailsApplication?.config?.smsConfig?.smsServiceAddress);

        try {
            http.request(POST) {

                body = bodyRequest;
                requestContentType = ContentType.JSON

                response.success = { resp ->
                    println "SUCCESS! ${resp.status}"

                    def user = RequestContextHolder.currentRequestAttributes().getSession().user

                    if (user != null) {
                        if (termin) {
                            termin.withTransaction {

                                try {
                                    // do desired action

                                } catch (e) {
                                    println(e);
                                }

                            }
                        }

                    }
                }
                response.failure = { resp ->
                    println "FAILURE request failed with status ${resp.status}, response body was [${resp.entity.content.text}]"
                    System.out << resp
                }
            }
        }
        //here error is caught
        catch (e) {
            log.error "Error: " + e;
        }
    }

我已尝试将此选项添加到 web.xml

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

这也是 WebXmlConfig.groovy

    listener.add = true
listener.classNames = ["org.springframework.web.context.request.RequestContextListener"]

但两者都没有帮助

【问题讨论】:

    标签: http grails


    【解决方案1】:

    这是没有很好记录的东西,或者您可以向您发送螺旋:

    试试this:

    /*
    * Fix for No thread-bound request found: Are you referring to request attributes
    */
    def webRequest = RequestContextHolder.getRequestAttributes()
    if(!webRequest) {
        def servletContext  = ServletContextHolder.getServletContext()
        def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
        webRequest =  grails.util.GrailsWebMockUtil.bindMockWebRequest(applicationContext)
    }
    

    你需要在你的 build.gradle 中进行 spring 测试

      compile 'org.springframework:spring-test:2.5'
    

    有一个 grails 2 分支,如果是 grails 2,则在另一个分支中遵循相同的类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2014-07-24
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多