Search code examples
javaspringkotlinspring-web

Spring server is not responding to a GET request


I want to create a Spring service that would return a text upon a GET request at http://localhost/version.

For this purpose, I wrote this code:

Controller

@Controller
@RequestMapping("/version")
class VersionController {
  @RequestMapping(method=arrayOf(RequestMethod.GET))
  fun version():String {
      return "1.0"
  }
}

Security configuration

@Configuration
open class SecurityConfiguration : WebSecurityConfigurerAdapter() {
  override fun configure(http:HttpSecurity) {
   http
                .authorizeRequests()
                .antMatchers("/version").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository
                        .withHttpOnlyFalse());
  }
}

App

@SpringBootApplication
open class App {
  fun run() {
      SpringApplication.run(App::class.java)
  }
}

fun main(args: Array<String>) {
  App().run()
}

When I compile (mvn compile), run (mvn exec:java -Dexec.mainClass=test.AppKt), and try to access http://localhost:8080/version) I get the 404 response.

Why? What part of the code do I need to change?


Solution

  • This code started to work, after I added the @RestController annotation to VersionController.