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
@RequestMapping("/version")
class VersionController {
@RequestMapping(method=arrayOf(RequestMethod.GET))
fun version():String {
return "1.0"
}
}
@Configuration
open class SecurityConfiguration : WebSecurityConfigurerAdapter() {
override fun configure(http:HttpSecurity) {
http
.authorizeRequests()
.antMatchers("/version").permitAll()
.anyRequest().authenticated()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository
.withHttpOnlyFalse());
}
}
@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?
This code started to work, after I added the @RestController
annotation to VersionController
.