首页 > 编程知识 正文

vuepost请求参数怎么传递,vue setup函数的作用是什么

时间:2023-05-06 19:50:59 阅读:274253 作者:879

PUT请求该如何传输请求参数呢?

有如下的接口

@RequestMapping(value = "testPut", method = RequestMethod.PUT)

public Result testPut(@RequestParam String foo, @RequestParam String bar) {

System.out.println(foo + " " + bar) ;

return Result.success(null);

}

通过CURL来调用该接口

# 通过-d传输请求参数失败

curl -X PUT -d "foo=foo&bar=bar" 'http://localhost:8080/testPut'

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'foo' is not present

# 但直接放在url后面是可以的

curl -X PUT 'http://localhost:8080/testPut?foo=foo&bar=bar'

但我想像POST一样 以请求体的方式传输请求参数, 而不是作为URL的一部分

现在我的方法是

@RequestMapping(value = "testPut2", method = RequestMethod.PUT)

public Result testPut2(@RequestBody FooBar fooBar) {

System.out.println(fooBar) ;

return Result.success(null);

}

@Data

static class FooBar{

public String foo;

public String bar;

}

然后以json的方式调用

curl -X PUT -H "Content-Type: application/json" -d '{"foo":"foo","bar":"bar"}' 'http://localhost:8080/testPut2'

这样是可以的 但奇怪为什么PUT只能接收json呢?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。