[英]How to pass input parameters using rest client in spring restful web services
I'm trying to create an web service using spring rest4, but not able to pass input parameters through rest-client, can anybody suggest me how to pass input parameters. 我正在尝试使用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());
}
}
I'm using "WizTool.org RestClient 3.2.2", Url = " http://localhost:8080/XYZ2/rest/user/create " & input parameters i'm passing as - 我正在使用“ 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"
}]
}
thanks in advance 提前致谢
Add the following to your 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;
}
Call this url " http://localhost:8080/XYZ2/rest/user/userformat " in your browser, it will return you user representation in json. 在浏览器中将此网址称为 “ http:// localhost:8080 / XYZ2 / rest / user / userformat ”,它将以json 格式返回用户表示形式。 Now copy this json format for the user and remove all the above code which was added to your controller.
现在为用户复制此json格式,并删除所有上述添加到您的控制器中的代码。
In wiztool rest client: 在wiztool rest客户端中:
1. enter the url " http://localhost:8080/XYZ2/rest/user/userformat ". 1.输入网址“ http:// localhost:8080 / XYZ2 / rest / user / userformat ”。 Assuming that you are deleting its corresponding controller mapping after this process
假设您要在此过程之后删除其对应的控制器映射
2. select method as GET 2.选择方法为GET
3. click the '>>' button to fire the request 3.点击“ >>”按钮以触发请求
4. copy whatever json you get in the HTTP response "Body" as shown in figure at the bottom 4.复制您在HTTP响应“正文”中获得的所有json,如底部图所示
User json might be something like the below one. 用户json可能类似于以下内容。 I have included only few properties.
我只包含了一些属性。 For rest of them you can get from output of the above introduced url "userformat".
对于它们的其余部分,您可以从上面介绍的网址“ userformat”的输出中获取。
{
"userId": 1,
"firstName": "ronaldinho",
"lastName": "Gaucho",
"userMailId": "r10@gmaildummy.com",
"roleId": 2
}
You can use this in request body for this url " http://localhost:8080/XYZ2/rest/user/create " 您可以在请求正文中为此网址使用“ http:// localhost:8080 / XYZ2 / rest / user / create ”
In wiztool rest client: 在wiztool rest客户端中:
1. enter the url "" http://localhost:8080/XYZ2/rest/user/create "" 1.输入网址“” http:// localhost:8080 / XYZ2 / rest / user / create “”
2. select method as POST 2.选择方法为POST
Why I am suggesting you this method is because I do not know if you have any json property annotations in User class. 为什么我建议您使用此方法,是因为我不知道您在User类中是否有任何json属性注释。 Also it is better to use this way than creating the json manually.
另外,使用这种方法比手动创建json更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.