-
Notifications
You must be signed in to change notification settings - Fork 115
/
ApiApplication.kt
54 lines (42 loc) · 1.69 KB
/
ApiApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package io.realworld
import io.realworld.jwt.ExposeResponseInterceptor
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration
@EnableCaching
@SpringBootApplication
class ApiApplication : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(exposeResponseInterceptor())
}
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(false)
.maxAge(3600)
super.addCorsMappings(registry)
}
@Bean
fun exposeResponseInterceptor() = ExposeResponseInterceptor()
@Bean
fun methodValidationPostProcessor(): MethodValidationPostProcessor {
val mv = MethodValidationPostProcessor()
mv.setValidator(validator())
return mv
}
@Bean
fun validator() = LocalValidatorFactoryBean()
}
fun main(args: Array<String>) {
SpringApplication.run(ApiApplication::class.java, *args)
}