Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy 在Ratpack中使用'prefix{}'方法而不是'all{byMethod{}}'会导致不允许使用405方法-what';怎么了?_Groovy_Ratpack - Fatal编程技术网

Groovy 在Ratpack中使用'prefix{}'方法而不是'all{byMethod{}}'会导致不允许使用405方法-what';怎么了?

Groovy 在Ratpack中使用'prefix{}'方法而不是'all{byMethod{}}'会导致不允许使用405方法-what';怎么了?,groovy,ratpack,Groovy,Ratpack,我刚刚开始阅读“Learn Ratpack”,在书的一开始的一个例子中,作者使用“all”、“byMethod”、“get”和“post”来举例说明如何解析请求数据,他这样做的方式,但我尝试使用“prefix”、“get”和“post”,但我无法得到相同的结果,它返回405方法不允许 我试图在文档中找到一些东西,但我不明白为什么会出现带有“前缀”的行为 示例版本 import static ratpack.groovy.Groovy.ratpack import ratpack.form.For

我刚刚开始阅读“Learn Ratpack”,在书的一开始的一个例子中,作者使用“all”、“byMethod”、“get”和“post”来举例说明如何解析请求数据,他这样做的方式,但我尝试使用“prefix”、“get”和“post”,但我无法得到相同的结果,它返回405方法不允许

我试图在文档中找到一些东西,但我不明白为什么会出现带有“前缀”的行为

示例版本

import static ratpack.groovy.Groovy.ratpack
import ratpack.form.Form

ratpack {
    handlers {
        all {
            byMethod {
                get {
                  //In the exemple he sends a html form
                }
                post {
                  //And here he parses it.
                }
            }
        }
    }
}
405版本

import static ratpack.groovy.Groovy.ratpack
import ratpack.form.Form

ratpack {
    handlers {
        prefix("parsing-request-data") {
            get{
               //From here all the same

就是这样,我遗漏了什么?

如果您想对同一相对路径使用多个不同的HTTP方法,您仍然需要使用
byMethod{}
方法创建这样的处理程序。否则,链中匹配相对路径的第一个处理程序将处理该请求,它可能会失败或成功。(在您的情况下,POST请求失败,不允许使用405方法,因为
get
处理程序处理该请求,并在请求中发现不正确的HTTP方法。如果您希望看到get请求失败,而不是POST one-reorder方法,那么
POST{}
是链中的第一个处理程序。)

这个
byMethod{}
方法允许为同一相对路径注册多个处理程序,这些处理程序将根据请求的HTTP方法进行解析。如果使用
前缀{}
方法,您可以在
路径{}
帮助程序方法中访问
byMethod{}
方法:

import static ratpack.groovy.Groovy.ratpack

ratpack {

    handlers {

        prefix("parsing-request-data") {
            path {
                byMethod {
                    post {
                        response.send("A response returned from POST /parsing-request-data\n ")
                    }

                    get {
                        response.send("A response returned from GET /parsing-request-data\n")
                    }
                }
            }

            get("test") {
                response.send("A response returned from GET /parsing-request-data/test\n")
            }
        }
    }
}
和几个curl命令来测试它:

$ curl -i -X GET http://localhost:5050/parsing-request-data    
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 51

A response returned from GET /parsing-request-data

$ curl -i -X POST http://localhost:5050/parsing-request-data    
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 53

A response returned from POST /parsing-request-data

$ curl -i -X GET http://localhost:5050/parsing-request-data/test
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 56

A response returned from GET /parsing-request-data/test