SpringBoot中访问GraphQL端点时出现404错误求助
问题描述
我是GraphQL新手,正在开发一个基于GraphQL的SpringBoot演示应用。尝试访问http://localhost:8080/graphql/schema.json端点时遭遇404白标错误,但可正常访问控制器中的/dogs端点。最初使用SpringBoot GraphQL Maven依赖,后续添加了其他GraphQL Maven依赖后问题仍未解决,恳请协助调试该错误。
应用启动类
@SpringBootApplication public class DogGraphqlApiApplication { public static void main(String[] args) { SpringApplication.run(DogGraphqlApiApplication.class, args); } }
application.properties配置
spring.jpa.defer-datasource-initialization=true spring.h2.console.enabled=true spring.h2.console.path=/h2 spring.datasource.url=jdbc:h2:mem:dogdata graphql.servlet.mapping=/graphql graphql.servlet.enabled=true graphql.servlet.corsEnabled=true graphiql.enabled=true graphiql.endpoint=/graphql graphiql.mapping=graphiql
pom.xml依赖片段
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.graphql</groupId> <artifactId>spring-graphql-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>5.0.2</version> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java-tools</artifactId> <version>5.2.4</version> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphiql-spring-boot-starter</artifactId> <version>5.0.2</version> </dependency>
GraphQL Schema文件(dog.graphqls)
type Dog { id: ID! name: String! breed: String! origin: String! } type Query { findAllDogs: [Dog]! findDogById(id:ID!): Dog! } type Mutation { deleteDogBreed(breed: String!) : Boolean updateDogName(newName: String!, id:ID!) : Dog! }
Controller类
@RestController public class DogController { private DogService dogService; @Autowired public void setDogService(DogService dogService){ this.dogService = dogService; } @GetMapping("/dogs") public ResponseEntity<List<Dog>> getAllDog(){ List<Dog> list = dogService.retrieveDogs(); return new ResponseEntity<List<Dog>>(list, HttpStatus.OK); } }
Query解析器
@Component public class Query implements GraphQLQueryResolver { private DogRepository dogRepository; public Query(DogRepository dogRepository){ this.dogRepository = dogRepository; } public Iterable<Dog> findAllDogs(){ return dogRepository.findAll(); } public Dog findDogById(Long id){ Optional<Dog> optionalDog = dogRepository.findById(id); if(optionalDog.isPresent()){ Dog dog = optionalDog.get(); return dog; }else{ throw new DogNotFoundException("Dog Not Found", id); } } }
Mutation解析器
@Component public class Mutation implements GraphQLMutationResolver { private DogRepository dogRepository; public Mutation(DogRepository dogRepository){ this.dogRepository = dogRepository; } public Boolean deleteDogBreed(String breed){ boolean deleted = false; Iterable <Dog> dogs = dogRepository.findAll(); for(Dog dog: dogs){ if(dog.getBreed().equals( breed) ){ dogRepository.delete(dog); deleted = true; } } if(!deleted){ throw new BreedNotFoundException("Breed Not Found", breed); } return deleted; } public Dog updateDogName(String newName, Long id){ Optional<Dog> optionalDog = dogRepository.findById(id); if(optionalDog.isPresent()){ Dog dog = optionalDog.get(); dog.setName(newName); dogRepository.save(dog); return dog; }else { throw new DogNotFoundException("Dog Not Found", id); } } }
错误页面

解决方案
1. 解决依赖冲突
你混合使用了Spring官方Spring GraphQL(org.springframework.graphql开头)和第三方graphql-java-boot-starter(com.graphql-java开头)两套不同的GraphQL实现,这是核心问题。
建议保留Spring官方依赖(更贴合SpringBoot生态),移除所有com.graphql-java开头的依赖,替换为以下官方依赖:
<!-- Spring GraphQL核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-graphql</artifactId> </dependency> <!-- Web依赖(用于HTTP端点) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- GraphQL测试依赖 --> <dependency> <groupId>org.springframework.graphql</groupId> <artifactId>spring-graphql-test</artifactId> <scope>test</scope> </dependency>
2. 调整Schema导出配置
Spring官方GraphQL默认没有/graphql/schema.json端点,需要手动启用并配置:
在application.properties中添加:
# 启用Schema导出 spring.graphql.schema.export.enabled=true # 指定导出路径为JSON格式 spring.graphql.schema.export.path=/graphql/schema.json spring.graphql.schema.export.format=json # 配置GraphiQL UI路径(可选) spring.graphql.graphiql.enabled=true spring.graphql.graphiql.path=/graphiql
3. 代码适配
- 确保Query和Mutation解析器使用Spring官方的
GraphQLQueryResolver和GraphQLMutationResolver(包路径为org.springframework.graphql.data.method.annotation),你的现有代码结构基本兼容,只需调整导入包即可。 - 将
dog.graphqls文件放在src/main/resources/graphql/目录下,Spring GraphQL会自动扫描该目录的Schema文件。
4. 验证步骤
- 清理Maven缓存,重新下载依赖
- 重启应用
- 访问
http://localhost:8080/graphql/schema.json验证是否能获取到Schema的JSON内容 - 访问
http://localhost:8080/graphiql测试GraphQL查询功能
内容的提问来源于stack exchange,提问作者Othello
相关产品推荐
相关产品推荐

