我正在我的Spring应用程序中使用新的图形for弹簧。我的问题是,当向/graphql端点发出POST请求时,服务器总是响应404。
这些是我的gradle依赖关系
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.flywaydb:flyway-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'我按照这个教程中的步骤创建控制器(以前称为解析器,实际上我将所有旧的Graphql解析器更改为新的@QueryMapping和@MutationMapping注释)
这是我的StepController代码
@Slf4j
@Controller
@RequiredArgsConstructor
public class StepController {
private final StepService stepService;
private final TokenService tokenService;
@QueryMapping
public ArrayList<Step> getSteps(String templateId) {
return stepService.getSteps(templateId);
}
@MutationMapping
public Step createStep(StepInput input, DataFetchingEnvironment env) {
return stepService.createStep(input, tokenService.getUserId(env));
}
}我还在我的项目中使用WebSecurity,通过在一个SecurityConfig.class中创建一个Bean,将被废弃的WebSecurityConfigurerAdapter更改为更现代的方法
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors()
.configurationSource(request -> {
var cors = new CorsConfiguration();
cors.setAllowedOrigins(List.of("http://localhost:4200", "electron://altair"));
cors.setAllowedMethods(List.of("GET", "POST", "DELETE", "OPTIONS"));
cors.setAllowedHeaders(List.of("*"));
cors.setAllowCredentials(true);
return cors;
})
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/graphql").permitAll()
.antMatchers(HttpMethod.GET, "/stomp/**").permitAll()
.anyRequest().authenticated()
.and()
// this disables session creation on Spring Security
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build();
}
}我的GraphQl模式是在文件夹结构中用resources/graphql构造的,在切换到新的Spring之前,我对此没有问题

UPDATE为了安全起见:我删除了整个文件夹结构,并将其替换为一个schema.graphqls文件,该文件只有Login突变和一个从未使用过的查询,只是为了避免Query needed编译器错误。
type Mutation {
login(email: String!, password: String!): Login!
}
type Login {
token: String
}
// unused, just to make the compiler happy
type Query {
getLogin: Login
}application.properties
spring.banner.location=classpath:logo.txt
spring.main.banner-mode=console
spring.output.ansi.enabled=ALWAYS
spring.main.allow-bean-definition-overriding=true
spring.mail.host=mail.blablabla
spring.mail.port=587
spring.mail.username=no-reply@blablabla
spring.mail.password=blablalba
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=false
spring.profiles.include=prod,dev
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.servlet.multipart.max-file-size=512KB我希望我提供了足够的信息,我的项目是如何设置的。我非常感谢任何能解决这个问题的帮助。昨天晚上我找了一个晚上,终于把这个问题发到这里来了。我希望有人能帮我。谢谢
发布于 2022-11-25 13:58:10
请在application.properties文件中添加此属性,然后再试一次。
spring.graphql.graphiql.enabled=true
https://stackoverflow.com/questions/74571399
复制相似问题