I am having trouble with a spring boot web grapql api project endpoint that keeps returning a 404 not found error message. My controller looks like this.
package com.pdrake.graphqlnfc.controllers
import com.pdrake.graphqlnfc.entities.movie.Movie
import com.pdrake.graphqlnfc.repositories.MovieRepository
import org.springframework.graphql.data.method.annotation.QueryMapping
import org.springframework.stereotype.Controller
@Controller
class MovieController(
private val movieRepository: MovieRepository
) {
@QueryMapping
fun movies(): List<Movie> {
return movieRepository.findAll()
}
}
My application.yml file looks like this.
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:default
password: *******
jpa:
hibernate:
ddl-auto: none
graphql:
graphiql:
enabled: true
cors:
allowed-origins: "http://localhost:8080"
allowed-headers: "*"
allowed-methods: GET, POST
security:
user:
name: pdrake
password: Rewind0614
My schema looks like this
schema {
query: Query
}
type Query {
movies: [Movie]!
movieById(id: ID!): Movie
}
type Movie {
id: ID
name: String!
description: String!
genres: [Genre]!
cast: [Cast]!
}
type Genre {
id: ID
name: String!
}
type Cast {
id: ID
name: String!
age: Int!
}
As well as a security configuration class
package com.pdrake.graphqlnfc.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.web.SecurityFilterChain
@Configuration
@EnableWebSecurity
class SecurityConfig {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http.authorizeHttpRequests {
it.anyRequest().permitAll()
}.csrf {
it.disable()
}.sessionManagement {
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
}
return http.build()
}
}
I have a build.gradle file that looks like this
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.jetbrains.kotlin.jvm' version '1.9.22'
id 'org.jetbrains.kotlin.plugin.spring' version '1.9.22'
id 'org.jetbrains.kotlin.plugin.jpa' version '1.9.22'
}
group = 'com.pdrake'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-graphql'
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 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
testImplementation 'org.springframework:spring-webflux'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.graphql:spring-graphql-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.withType(KotlinCompile) {
kotlinOptions {
freeCompilerArgs += '-Xjsr305=strict'
jvmTarget = '17'
}
}
tasks.named('test') {
useJUnitPlatform()
}
The request I am send via Postman is shown here.
I originally thought it was a security issue so I created the filterChain bean, and that didn’t do anything. I saw something on the online that says you need to implement an interface in your controller class, however that is in conflict with the official documentation that says all you have to do is annotate your class with @Controller. So basically I am completely out of ideas.