【问题标题】:Post method not working with micronaut frameworkPost 方法不适用于 micronaut 框架
【发布时间】:2020-11-06 16:01:32
【问题描述】:

我对 micronaut 和端点概念还很陌生,但是我对某些东西感到好奇。我的 get 方法有效并返回了预期的结果,但是,当我使用 postman 测试我的 post 方法时,我收到 405 method not allowed 错误。我意识到我可能没有正确使用 post 方法,但我不太确定如何正确使用 post。下面是我的代码

 @Controller("/test")
    class Controller {
        @Get
        @Produces(MediaType.TEXT_PLAIN)
        fun index(): String {
            return "Its working"
        }
    
        @Post
        @Produces(MediaType.TEXT_PLAIN)
        fun dopost(): String{
            return "ok"
        }
    }

【问题讨论】:

    标签: api kotlin postman endpoint micronaut


    【解决方案1】:

    https://github.com/jeffbrown/testtestmnpost查看项目。

    https://github.com/jeffbrown/testtestmnpost/blob/ba1ecd3099e91a9f87112a1bd7df1ec113bfc055/src/main/kotlin/testtestmnpost/Controller.kt

    package testtestmnpost
    
    import io.micronaut.http.annotation.Controller
    import io.micronaut.http.annotation.Get
    import io.micronaut.http.HttpStatus
    import io.micronaut.http.MediaType
    import io.micronaut.http.annotation.Post
    import io.micronaut.http.annotation.Produces
    
    @Controller("/test")
    class Controller {
        @Get
        @Produces(MediaType.TEXT_PLAIN)
        fun index(): String {
            return "Its working"
        }
    
        @Post
        @Produces(MediaType.TEXT_PLAIN)
        fun dopost(): String{
            return "ok"
        }
    }
    

    这两种方法都有效:

     $ http :8080/test     
    HTTP/1.1 200 OK
    Date: Fri, 6 Nov 2020 16:26:35 GMT
    connection: keep-alive
    content-length: 11
    content-type: text/plain
    
    Its working
    
     $ 
     $ http POST :8080/test
    HTTP/1.1 200 OK
    Date: Fri, 6 Nov 2020 16:26:40 GMT
    connection: keep-alive
    content-length: 2
    content-type: text/plain
    
    ok
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      相关资源
      最近更新 更多