Spring引导启动器图表ql:正在为/graphql端点获取404

z9smfwbn  于 2022-11-29  发布在  Spring
关注(0)|答案(1)|浏览(163)

我在我的Spring Boot应用程序中使用新的Spring for Graphql。我的问题是,当向/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'

我按照tutorial中的步骤创建了控制器(以前称为解析器,实际上我将所有旧的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更改为more modern way of doing this

@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 Boot Graphql之前,我对此没有任何问题

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
}

应用程序.属性

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

我希望我提供了足够的信息,我的项目是如何建立的。我真的很感激任何帮助,可以解决这个问题。我搜索了整个晚上昨天,终于现在张贴在这里这个问题。我希望有人能帮助我。谢谢

qncylg1j

qncylg1j1#

请在www.example.com文件中添加此属性application.properties,然后重试
启用=真

相关问题