java 使用邮递员测试具有 RequestParam 的休息服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43499462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test rest service having RequestParam using postman
提问by Ne AS
I want to test my REST service in order to save a product having a certain category (manyToOne) with Postman:
我想测试我的 REST 服务,以便使用 Postman 保存具有特定类别(多对一)的产品:
This is the body of my request:
这是我的请求正文:
{
"categoryId": 36,
"product": {
"code": "code1",
"name": "product1",
"price": 20
}
}
And this is how the signature of my REST service method looks like:
这就是我的 REST 服务方法的签名的样子:
@RequestMapping(value = "/addProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ProductBean> add(@RequestParam(value ="categoryId") Long id, @RequestParam(value = "product") ProductBean productBean)
I put in Postman my URL with the /addProduct
at the end, then I choose POST
. In the body tab, I choose raw
and select the JSON (application json)
.
When I send the request I got HTTP 400.
我把我的 URL 放在 Postman/addProduct
最后,然后我选择POST
. 在正文选项卡中,我选择raw
并选择JSON (application json)
. 当我发送请求时,我得到了 HTTP 400。
How can test this without error in Postman?
如何在 Postman 中没有错误地测试这个?
Edit
编辑
I want to test it using postman to be sure that my REST is working before adding the front part. This is how I will send the data from the front
我想使用邮递员测试它以确保我的 REST 在添加前部之前正常工作。这就是我从前端发送数据的方式
add: function (product, id, successCallBack, failureCallBack) {
$http({
method: 'POST',
url: "/addProduct",
params: {
product: product,
categoryId: id
},
headers: {'Content-Type': 'application/json'}
}).then(successCallBack, failureCallBack);
}
回答by jny
Your method signature is incorrect. @RequestParam is the parameter in the uri, not the body of the request. It should be:
您的方法签名不正确。@RequestParam 是 uri 中的参数,而不是请求的正文。它应该是:
public ResponseEntity<ProductBean> add(MyBean myBean)
where MyBean
has to properties: id and product or
哪里 MyBean
必须有属性:id 和 product 或
public ResponseEntity<ProductBean> add(@ModelAttribute(value ="categoryId") Long id, @ModelAttribute(value = "product") ProductBean productBean)
For more on mapping requests see Spring documentation
有关映射请求的更多信息,请参阅Spring 文档
If you want to stick to your original mapping, then everything should be passed in the query string and nothing in the body. The query string will look something like that:
如果你想坚持你的原始映射,那么一切都应该在查询字符串中传递,而在正文中没有任何内容。查询字符串看起来像这样:
/addProduction?categoryId=36&product={"code":"code1",...}