【问题标题】:Problem with spring boot graphql. Request /graphql results with 404春季启动graphql的问题。使用 404 请求 /graphql 结果
【发布时间】:2019-05-07 23:23:26
【问题描述】:

我正在尝试运行最简单的 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'
}

DemoApplication.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);
    }
}

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

【问题讨论】:

  • 你检查你请求的端口了吗? Spring 在收到请求时是否会记录任何内容?
  • 是的,我检查了端口。 404 表示服务器收到请求。
  • 没错,我的错。您在依赖项中拥有的这个 graphql 包应该自动公开端点 /graphql 吗?我看到它在您的部门中,但没有关于 graphql api 的进一步配置。
  • 文档很乱,在这里github.com/graphql-java-kickstart/graphql-spring-boot 找不到任何最小的工作示例。我一定是错过了什么重要的事情。

标签: java spring-boot graphql


【解决方案1】:

我在使用 graphiql 和 Swagger 时遇到了同样的问题 页面 graphiql 让我在浏览器控制台上出现 404 错误。问题是无法从供应商资源中获取 js 库。

开启class SwaggerConfig extends WebMvcConfigurationSupport

我做到了:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
//This is for graphiql works
registry.addResourceHandler("/vendor/**")
      .addResourceLocations("classpath:/static/vendor/");

}

【讨论】:

    【解决方案2】:

    文档 (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();
        }
    }
    

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

    【讨论】:

    • 不起作用,在启动时抛出另一个错误,说您需要一个 GraphQL 类型的 bean
    猜你喜欢
    • 2018-01-13
    • 2020-10-23
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2016-01-28
    • 2020-07-02
    • 1970-01-01
    • 2014-01-09
    相关资源
    最近更新 更多