我正在尝试使用 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"
}]
}
提前致谢
最佳答案
将以下内容添加到您的 Controller ,
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;
}
在浏览器中调用此网址“http://localhost:8080/XYZ2/rest/user/userformat ”,它将返回 json 格式的用户表示形式。 现在为用户复制此 json 格式并删除添加到 Controller 的所有上述代码。
在wiztool Rest客户端中:
1. 输入网址“http://localhost:8080/XYZ2/rest/user/userformat”。假设您在此过程之后删除其相应的 Controller 映射
2.选择方法为GET
3. 单击“>>”按钮触发请求
4.复制您在HTTP响应“Body”中获得的任何json,如下图所示
用户 json 可能类似于以下内容。我只包含了一些属性。对于其余的内容,您可以从上面引入的 url“userformat”的输出中获取。
{
"userId": 1,
"firstName": "ronaldinho",
"lastName": "Gaucho",
"userMailId": "r10@gmaildummy.com",
"roleId": 2
}
您可以在请求正文中使用此网址“http://localhost:8080/XYZ2/rest/user/create”
在wiztool Rest客户端中:
1.输入网址“”http://localhost:8080/XYZ2/rest/user/create “”
2.选择方法为POST
- 现在单击“正文”,如图中突出显示的那样。从下拉列表中选择“字符串主体”。在下一个输入中,输入内容类型“application/json”。现在粘贴我们从第一个 url 的输出中复制的用户 json 格式并将其粘贴到文本区域中。你可以检查下图
- 点击“>>”触发请求。
为什么我建议你这个方法是因为我不知道你的 User 类中是否有任何 json 属性注释。而且使用这种方式比手动创建 json 更好。
关于java - 如何在 Spring Restful Web 服务中使用 Rest 客户端传递输入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436175/