简体   繁体   English

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

[英]Problem with spring boot graphql. Request /graphql results with 404

I'm trying to run simplest graphql example.我正在尝试运行最简单的 graphql 示例。 I created application with spring initializer and only added graphql dependencies.我使用 spring 初始化器创建了应用程序,并且只添加了 graphql 依赖项。 My build.gradle我的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演示应用程序.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);
    }
}

When I run the project and hit the endpoint /graphql it returns 404 .当我运行项目并点击端点/graphql时,它返回404 What is missing in my configuration?我的配置中缺少什么?

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

The servlet becomes accessible at /graphql if graphql-spring-boot-starter added as a dependency to a boot application and a GraphQLSchema bean is present in the application.如果 graphql-spring-boot-starter 作为依赖项添加到启动应用程序并且应用程序中存在 GraphQLSchema bean,则可以在 /graphql 访问 servlet。

...and the minimum example it links to looks like this: ...它链接到的最小示例如下所示:

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

So you're missing a Graphql schema to be used.所以您缺少要使用的 Graphql 架构。 It says if there is one, the API endpoint will be exposed automatically.它说如果有,API 端点将自动公开。
Good luck!祝你好运!

I had a equal problem using graphiql and Swagger The page graphiql got me erros 404 on browser console.我在使用 graphiql 和 Swagger 时遇到了同样的问题 graphiql 页面让我在浏览器控制台上出现错误 404。 The problem was that cannot get the js library from vendor resource.问题是无法从供应商资源中获取 js 库。

On class SwaggerConfig extends WebMvcConfigurationSupportclass SwaggerConfig extends WebMvcConfigurationSupport

I did:我做了:

@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/");

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM