fix: send proper JSON on API 404 instead of index.html

bug introduced in bb183828a1
This commit is contained in:
Gauthier Roebroeck 2020-02-25 14:10:29 +08:00
parent ad07c4bb91
commit fb147a447a
2 changed files with 26 additions and 17 deletions

View file

@ -1,15 +1,14 @@
package org.gotson.komga.infrastructure.web
import org.springframework.boot.web.server.ErrorPage
import org.springframework.boot.web.server.WebServerFactoryCustomizer
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
import org.springframework.context.annotation.Configuration
import org.springframework.http.CacheControl
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.servlet.NoHandlerFoundException
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import java.util.concurrent.TimeUnit
@Configuration
@ -25,18 +24,29 @@ class StaticResourceConfiguration : WebMvcConfigurer {
"classpath:public/favicon.ico"
)
.setCacheControl(CacheControl.noStore())
}
override fun addViewControllers(registry: ViewControllerRegistry) {
registry.addViewController("/notFound")
.setStatusCode(HttpStatus.OK)
.setViewName("forward:/")
registry
.addResourceHandler(
"/css/**",
"/fonts/**",
"/img/**",
"/js/**"
)
.addResourceLocations(
"classpath:public/css/",
"classpath:public/fonts/",
"classpath:public/img/",
"classpath:public/js/"
)
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic())
}
}
@Component
class CustomContainer : WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
override fun customize(factory: ConfigurableServletWebServerFactory) {
factory.addErrorPages(ErrorPage(HttpStatus.NOT_FOUND, "/notFound"))
@ControllerAdvice
class Customizer {
@ExceptionHandler(NoHandlerFoundException::class)
fun notFound(): String {
return "forward:/"
}
}

View file

@ -7,10 +7,7 @@ spring:
console:
enabled: true
resources:
cache:
cachecontrol:
cache-public: true
max-age: 365d
add-mappings: false
jpa.properties:
javax:
persistence.sharedCache.mode: ENABLE_SELECTIVE
@ -24,6 +21,8 @@ spring:
region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
thymeleaf:
prefix: classpath:/public/
mvc:
throw-exception-if-no-handler-found: true
server.servlet.session.timeout: 7d
management: