错误Whitelabel错误页:使用级别1以上的映射进行查询时出现意外错误(类型=未找到,状态=404)



在我看来,面临着一个极其模糊和难以理解的问题。我有一个关于Spring Boot+Kotlin的应用程序。以前,该应用程序对Rest控制器有例外;最近需要在响应中提供html,因此添加了一个常规控制器。但是,当将此控制器映射到一个以上级别时,所有请求(超过一个级别(都会导致错误:

<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解析静态页面的方式
由于"/welcome/welcome"是嵌套的,您需要使用资源的正确相对路径或绝对路径。

@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {
@GetMapping("/welcome")
fun signIn(): String {
return "../index.html"
}
}

@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {
@GetMapping("/welcome")
fun signIn(): String {
return "/index.html"
}
}

相关内容

最新更新