【发布时间】:2020-03-04 23:16:42
【问题描述】:
在我看来,面临一个极其模棱两可和难以理解的问题。 我在 Spring Boot + Kotlin 上有一个应用程序。以前,应用程序对 Rest 控制器有异常;最近需要在响应中提供 html,因此添加了一个常规控制器。但是,当将此控制器映射到超过 1 个级别时 - 所有请求(超过 1 个级别)都会导致错误:
<html>
<body>
<h1> Whitelabel Error Page </h1>
<p> This application has no explicit mapping for / error, so you are seeing this as a fallback. </p>
<div> There was an unexpected error (type = Not Found, status = 404). </div>
<div> No message available </div>
</body>
</html>
此外,第一级的请求可以正常运行。
完全无法理解这与什么有关。大量尝试解决此问题(没有任何帮助),但可能遗漏了一些东西。
我应用与可能的问题相关联的设置(如果突然有人试图提供帮助并且需要其他东西 - 告诉我,我会添加此信息)
控制器
@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {
@GetMapping
fun welcome(): String {
return "index.html"
}
@GetMapping("/welcome")
fun signIn(): String {
return "index.html"
}
}
WebMvcConfig
@Configuration
@EnableWebMvc
class WebMvcConfig : WebMvcConfigurer {
private final val classpathResourceLocations = arrayOf(
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
)
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**")
.addResourceLocations(*classpathResourceLocations)
}
}
override fun addViewControllers(registry: ViewControllerRegistry) {
registry.addViewController("/").setViewName("index.html")
}
@Bean
fun internalResourceViewResolver(): ViewResolver {
val viewResolver = InternalResourceViewResolver()
viewResolver.setViewClass(InternalResourceView::class.java)
return viewResolver
}
}
WebSecurityConfig
@Configuration
@EnableWebSecurity
class CommonSecurityConfig : WebSecurityConfigurerAdapter() {
private val permitPatterns = arrayOf(
"/",
"/welcome/**",
"/resources/**",
"/actuator/health**",
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
)
override fun configure(http: HttpSecurity) {
super.configure(http)
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.cors().configurationSource(corsConfigurationSource())
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(*permitPatterns).permitAll()
.antMatchers("/api/internal/**").hasAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.addFilterAfter(filter(), UsernamePasswordAuthenticationFilter::class.java)
}
// ... some logic after ...
}
因此,如果我沿着路径http://localhost:8080/welcome 执行请求,我将得到index.html 页面
如果我沿着路径 http://localhost:8080/welcome/welcome 执行请求 - 我得到上面的错误
index.html 文件位于路径 src/main/resources/static/index.html
【问题讨论】:
标签: spring-boot spring-mvc kotlin