代码之家  ›  专栏  ›  技术社区  ›  user2121

如何对返回整数的方法执行post请求

  •  0
  • user2121  · 技术社区  · 5 年前

    我试着做了如下的发帖请求:

    http://localhost:8080/root/initVar/id/2->error
    http://localhost:8080/root/initVar/id?2->error
    

    但是当我从方法中返回Int数据类型时,它不起作用。 请让我知道如何修正这个错误。

    请注意,这个方法返回Int

    :

    @RestController
    @RequestMapping("root")
    class Controller1 {
    
    val sayHiLazy by lazy {
        "hi from get"
    }
    
    @GetMapping(value = "/sayHi")
    @ResponseBody
    fun  sayHi() : String {
        return sayHiLazy
    }
    
    @PostMapping("/initVar/id")
    @ResponseBody
    fun initVar(@PathVariable id : Int) : Int {
        return 11
    }
    }to use spring annotation with koltin in intellij. i used spring annotation with java projects in intellij. but for koltin, it does not work.
    

    在下面的示例中,HelloController带有红色下划线,我收到以下警告:

        classes annotated with @Configurations cannto be implicily subclassed and must be final
    

    请让我知道怎么做

    控制器1

    @SpringBootConfiguration
    @RequestMapping("/app")
    public class HelloController {
    
    @RequestMapping(value = "/hello")
    fun SayHello(): String {
        return "success"
    }
    }
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Michal W    5 年前
    @RequestMapping(value="/stuff/{id}")
    @ResponseBody
    public String doStuff(@PathVariable("id") int id){
        return "id="+id;
    }
    
    1. 您通常编写POST方法,末尾不带变量。对于这种情况,您应该使用PUT- https://restfulapi.net/rest-put-vs-post/

    2. String.valuOf(id);

    当做