spring boot graphql 的问题。使用 404 请求 /graphql 结果

Woj*_*cki 5 java spring-boot graphql

我正在尝试运行最简单的 graphql 示例。我使用 spring 初始化程序创建了应用程序,并且只添加了 graphql 依赖项。我的build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')

    compile 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.3.1'
    compile 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.3.1'
    compile 'com.graphql-java-kickstart:voyager-spring-boot-starter:5.3.1'
}
Run Code Online (Sandbox Code Playgroud)

演示应用程序.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行项目并点击端点时,/graphql它返回404. 我的配置中缺少什么?

mum*_*itz 9

文档(https://github.com/graphql-java-kickstart/graphql-spring-boot#enable-graphql-servlet)说:

如果将 graphql-spring-boot-starter 作为依赖项添加到引导应用程序并且应用程序中存在 GraphQLSchema bean,则可以在 /graphql 访问该 servlet。

...以及它链接到的最小示例如下所示:

@SpringBootApplication
public class ApplicationBootConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationBootConfiguration.class, args);
    }

    @Bean
    GraphQLSchema schema() {
        return GraphQLSchema.newSchema()
            .query(GraphQLObjectType.newObject()
                .name("query")
                .field(field -> field
                    .name("test")
                    .type(Scalars.GraphQLString)
                    .dataFetcher(environment -> "response")
                )
                .build())
            .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,您缺少要使用的 Graphql 架构。它说如果有,API 端点将自动公开。
祝你好运!

  • 不起作用,在启动时抛出另一个错误,指出您需要 GraphQL 类型的 bean (2认同)