使用邮递员的RequestParam测试休息服务

时间:2017-04-19 15:02:52

标签: java json spring-boot postman

我想测试我的REST服务,以便使用Postman保存具有特定类别(manyToOne)的产品:

这是我的要求的主体:

{
    "categoryId": 36,
    "product": {
        "code": "code1",
        "name": "product1",
        "price": 20
    }
}

这就是我的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)

我在Postman的最后用/addProduct添加了我的网址,然后选择了POST。在正文标签中,我选择raw并选择JSON (application json)。 当我发送请求时,我得到了HTTP 400.

如何在Postman中无错误地测试?

修改

我想使用邮递员测试它,以确保我的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);
    }

1 个答案:

答案 0 :(得分:5)

您的方法签名不正确。 @RequestParam是uri中的参数,而不是请求的主体。它应该是:

 public ResponseEntity<ProductBean> add(MyBean myBean)

其中MyBean属性:id和product或

public ResponseEntity<ProductBean> add(@ModelAttribute(value ="categoryId") Long id, @ModelAttribute(value = "product") ProductBean productBean)

有关映射请求的更多信息,请参阅Spring documentation

如果你想坚持你的原始映射,那么一切都应该在查询字符串中传递,而在正文中没有任何内容。 查询字符串看起来像这样:

/addProduction?categoryId=36&product={"code":"code1",...}
相关问题