1

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?

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325

2 Answers2

-1

Why are you using "(method=arrayOf(RequestMethod.GET))"? try using "(method=RequestMethod.GET)" and it should work. ot you can use @GET annotation on method itself

-1

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

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325