Spring 服务器没有响应 GET 请求
Spring server is not responding to a GET request
我想创建一个 Spring 服务,该服务将 return 根据 http://localhost/version
的 GET 请求发送文本。
为此,我写了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()
}
当我编译 (mvn compile
)、运行 (mvn exec:java -Dexec.mainClass=test.AppKt
) 并尝试访问 http://localhost:8080/version
) 时,我得到 404 响应。
为什么?我需要更改代码的哪一部分?
为什么要使用“(method=arrayOf(RequestMethod.GET))”?
尝试使用“(method=RequestMethod.GET)”,它应该可以工作。
不是你可以在方法本身上使用@GET 注释
在我将 @RestController
注释添加到 VersionController
之后,此代码开始工作。
我想创建一个 Spring 服务,该服务将 return 根据 http://localhost/version
的 GET 请求发送文本。
为此,我写了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()
}
当我编译 (mvn compile
)、运行 (mvn exec:java -Dexec.mainClass=test.AppKt
) 并尝试访问 http://localhost:8080/version
) 时,我得到 404 响应。
为什么?我需要更改代码的哪一部分?
为什么要使用“(method=arrayOf(RequestMethod.GET))”? 尝试使用“(method=RequestMethod.GET)”,它应该可以工作。 不是你可以在方法本身上使用@GET 注释
在我将 @RestController
注释添加到 VersionController
之后,此代码开始工作。