Kotlin+Spring+GraphQL:405方法不允许

e4yzc0pl  于 2023-02-04  发布在  Spring
关注(0)|答案(1)|浏览(326)

我正在使用Spring boot + GrapQL构建一个简单的示例

package com.example.graphql

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.graphql.data.method.annotation.QueryMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class GraphqlApplication

fun main(args: Array<String>) {
    runApplication<GraphqlApplication>(*args)
}

@RestController
class MyQueryController {

    @QueryMapping
    fun message(): Message = Message("1","some-text")

}

data class Message(var id: String, var text: String) {
}

然后我在src/main/resources/graphql/schema. graphqls下得到了这个模式,如下所示

type Query {
    message: Message
}
type Message {
    id: String
    text: String
}

当我尝试访问http://localhost:8080/graphql时,我得到[95221568 - 1]已完成405方法不允许,标题= {masked}
有线索吗?
这是pom.xml中的依赖项

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    ....
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.kotlin</groupId>
            <artifactId>reactor-kotlin-extensions</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-coroutines-reactor</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    ....
nukf8bse

nukf8bse1#

正如@ d. j. brown在评论中所解释的,Spring for GraphQL不支持"/graphql"端点上的GET查询,尽管其他实现可以支持它,但它只支持POST HTTP请求。
如果你想探索你的API,你可以通过spring.graphql.graphiql.enabled=true在配置属性中启用graphiql UI,然后浏览http://localhost:8080/graphiql,这样你就可以使用这个Playground工具创建和发送GraphQL查询。

相关问题