java—SpringBootGraphQL有问题带有404的请求/graphql结果

fxnxkyjh  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(944)

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

}

演示应用程序.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` . 我的配置中缺少什么?
xn1cxnb4

xn1cxnb41#

我在使用graphiql和swagger时遇到了同样的问题graphiql在浏览器控制台上得到了erros 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/");

}

bjp0bcyl

bjp0bcyl2#

文件(https://github.com/graphql-java-kickstart/graphql-spring-boot#enable-graphql servlet)说:
如果graphql spring boot starter作为启动应用程序的依赖项添加,并且应用程序中存在graphqlschema bean,那么servlet将在/graphql处可访问。
…它链接到的最小示例如下所示:

@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端点将自动公开。
祝你好运!

相关问题