SpringBoot多模块应用中Netflix GraphQL服务404问题排查求助
排查Netflix DGS GraphQL接口404问题
问题现象
访问/graphql接口返回404错误:
{ "timestamp": "2023-01-25T18:25:00.762+00:00", "status": 404, "error": "Not Found", "message": "No message available", "path": "/graphql" }
执行的查询语句:
query{ retailers{ ...allfields } }
环境配置
build.gradle.kts 配置
plugins { `java-library` id("app-java-conventions") id("com.netflix.dgs.codegen") id("com.google.cloud.tools.jib") // for jib agent look into https://github.com/GoogleContainerTools/jib/tree/master/examples/java-agent } dependencyManagement { imports { mavenBom("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release") } } dependencies { implementation(project(":ky-launch-darkly")) implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release")) implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter") implementation("com.netflix.graphql.dgs:graphql-dgs-extended-scalars") implementation("com.netflix.graphql.dgs:graphql-dgs-subscriptions-websockets-autoconfigure") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter") // 重复依赖 implementation("net.devh:grpc-client-spring-boot-starter:2.13.1.RELEASE") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("net.datafaker:datafaker:1.7.0") }
权限配置
@Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/**").permitAll() .and() .requestCache() .requestCache(new NullRequestCache()) .and() .headers() .frameOptions().sameOrigin() // needed for H2 web console .and() .sessionManagement() .maximumSessions(1) .maxSessionsPreventsLogin(true) .sessionRegistry(sessionRegistry()); }
Resolver实现
@DgsComponent public class RetailerDataResolver { @DgsQuery public List<Retailer> retailers(){ return RetailerDatasource.RETAILER_LIST; } }
Schema目录结构
Schema文件位于src/main/resources/schema目录下。
可能的原因及解决办法
1. Schema文件路径不符合DGS默认约定
Netflix DGS框架默认扫描src/main/resources/graphqls目录下的Schema文件,你的Schema放在schema目录下,框架无法自动识别。
解决办法:
- 方案一:将
schema目录重命名为graphqls,沿用默认路径无需额外配置。 - 方案二:在
application.yml或application.properties中指定Schema扫描路径:dgs: graphql: schema-location: classpath*:schema/**/*.graphqls
2. 重复依赖导致冲突
build.gradle.kts中重复引入了graphql-dgs-spring-boot-starter,可能引发依赖版本冲突或组件加载异常。
解决办法:
删除其中一行重复的依赖声明,修改后依赖块如下:
dependencies { implementation(project(":ky-launch-darkly")) implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release")) implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter") implementation("com.netflix.graphql.dgs:graphql-dgs-extended-scalars") implementation("com.netflix.graphql.dgs:graphql-dgs-subscriptions-websockets-autoconfigure") implementation("org.springframework.boot:spring-boot-starter-web") implementation("net.devh:grpc-client-spring-boot-starter:2.13.1.RELEASE") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("net.datafaker:datafaker:1.7.0") }
3. 多模块环境下组件扫描未覆盖Resolver所在包
如果这是子模块,主应用的@SpringBootApplication可能未扫描到当前子模块中@DgsComponent标注的Resolver类,导致框架无法识别查询处理器。
解决办法:
在主应用启动类上添加组件扫描配置,覆盖Resolver所在的包路径:
@SpringBootApplication(scanBasePackages = {"com.your.main.package", "com.your.graphql.module.package"}) public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
或使用@ComponentScan注解指定需要扫描的包。
4. 自定义了GraphQL端点路径
检查application.yml中是否配置了自定义的GraphQL端点路径,如果有,需使用配置后的路径访问:
dgs: graphql: path: /your-custom-graphql-path
若存在该配置,访问时需替换为对应路径,而非默认的/graphql。
内容的提问来源于stack exchange,提问作者justrying
相关产品推荐
相关产品推荐

