如何在 spring restful Web 服务中使用 rest 客户端传递输入参数

How to pass input parameters using rest client in spring restful web services

我正在尝试使用 spring rest4 创建 Web 服务,但无法通过 rest-client 传递输入参数,谁能建议我如何传递输入参数。

RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status addEmployee(@RequestBody User user) {
    try {
        // Manually setting passing parameters
        user.setUserId(0);
        user.setFirstName("dfsdfsd");
        user.setUserMailId("sadsda");
        user.setAddress("sfds");
        user.setCreatedBy(1);
        user.setIsDeleted(0);
        user.setLastName("sadas");
        user.setMobileNumber("adsasdsa");
        user.setUsrtStatus(1);
        user.setUserPassword("asdsaf");
        user.setRoleId(1);
        System.out.println("firstname:=" + user);

        dataServices.addEntity(user);

        System.out.println(user);
        return new Status(1, "Employee added Successfully !");
    } catch (Exception e) {
        e.printStackTrace();
        return new Status(0, e.toString());
    }

}

我正在使用 "WizTool.org RestClient 3.2.2", Url = "http://localhost:8080/XYZ2/rest/user/create" & 我传递的输入参数 -

{
    "inputs": [{
        "parameterName": "pv_user_id",
        "parameterValue": ""
    }{
        "parameterName": "pv_adress",
        "parameterValue": "kjhfgkjdfhgfdhk"
    }]
}

提前致谢

将以下内容添加到您的控制器,

RequestMapping(value = "/userformat", method = RequestMethod.GET)
public @ResponseBody User getUserFormat() {
    User user = new User();
    user.setUserId(0);
    user.setFirstName("dfsdfsd");
    user.setUserMailId("sadsda");
    user.setAddress("sfds");
    user.setCreatedBy(1);
    user.setIsDeleted(0);
    user.setLastName("sadas");
    user.setMobileNumber("adsasdsa");
    user.setUsrtStatus(1);
    user.setUserPassword("asdsaf");
    user.setRoleId(1);

    return user;
}

在您的浏览器中将此 url 称为“http://localhost:8080/XYZ2/rest/user/userformat”,它将 return 您在 json 中的用户表示。 现在为用户复制此 json 格式并删除添加到控制器的所有上述代码。

在 wiztool rest 客户端中:
1.输入url“http://localhost:8080/XYZ2/rest/user/userformat”。假设你在这个过程之后删除它对应的控制器映射
2. select 方法作为 GET
3. 点击“>>”按钮触发请求
4. 复制你在HTTP响应"Body"中得到的任何json,如下图

用户 json 可能类似于下面的用户。我只包含了几个属性。对于其余部分,您可以从上面介绍的 url "userformat".

的输出中获得
{
"userId": 1,
"firstName": "ronaldinho",
"lastName": "Gaucho",
"userMailId": "r10@gmaildummy.com",
"roleId": 2
}

您可以在请求正文中使用此 url "http://localhost:8080/XYZ2/rest/user/create"

在 wiztool rest 客户端中:
1.输入url""http://localhost:8080/XYZ2/rest/user/create""
2. select方法为POST

  1. 现在单击图像中突出显示的 "Body"。从下拉列表中选择 "String body"。在下一个输入中,输入内容类型 "application/json"。现在粘贴我们从第一个 url 的输出中复制的用户 json 格式并将其粘贴到文本区域。你可以检查下图
  2. 点击“>>”触发请求。

为什么我建议你这个方法是因为我不知道你在用户class中是否有任何json 属性注释。此外,使用这种方式比手动创建 json 更好。