【问题标题】:Kotlin Spring Boot CORS "No mapping for OPTIONS /users"Kotlin Spring Boot CORS“选项/用户没有映射”
【发布时间】:2021-10-24 05:44:35
【问题描述】:

对于我的问题,我已经尝试了所有可以在堆栈溢出时找到的解决方案,但没有任何效果。

我的 kotlin 应用中有一个 CORS 配置。

@Configuration
@EnableWebMvc
class WebConfig : WebMvcConfigurer {
    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .allowedOrigins("*")
            .allowedHeaders("*");
    }
}

我有这个控制器:

@RestController
@RequestMapping("users")
class UserController(
    val userService: UserService
) {
    @CrossOrigin
    @PostMapping("/", produces = ["application/json"])
    fun createUser(@RequestBody user: User): User {
        return userService.createUser(user)
    }
}

当我从 Angular 应用程序调用该端点时,我收到以下错误:

WARN 19441 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping for OPTIONS /users

我似乎无法让全球 CORS 工作。你是如何在 Kotlin 中做到这一点的?

谢谢!

【问题讨论】:

  • 如果你删除@CrossOrigin会发生什么?

标签: spring-boot rest kotlin cors


【解决方案1】:

您调用的网址不正确。

在您的@PostMapping 中,您注册了/users/ 而不是/users

要解决此问题,请将您的帖子映射更改为 @PostMapping(produces = ["application/json"]),它应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2016-03-03
    • 2019-08-22
    • 2020-07-21
    • 2021-05-06
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多