简体   繁体   English

如何在Spring Restful Web Services中使用Rest Client传递输入参数

[英]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,如底部图所示
wiztool rest客户端:获取用户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

在此处输入图片说明

  1. now click on "Body" as highlighted in image. 现在单击“主体”,如图中突出显示。 choose "String body" from dropdown. 从下拉列表中选择“字符串主体”。 in the next input, enter "application/json" which is content-type. 在下一个输入中,输入内容类型的“ application / json”。 Now paste the user json format which we copied from the output of first url and paste it in the text area. 现在粘贴从第一个url的输出中复制的用户json格式,并将其粘贴到文本区域中。 you can check with the below image 您可以检查下图 在此处输入图片说明
  2. click '>>' to fire the request. 点击“ >>”以触发请求。

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.

相关问题 如何使用模型类作为输入将输入参数传递给Spring Restful Web服务方法并执行MySql存储过程 - How to Pass input parameters to Spring restful web service method using model class as inputs & execute MySql stored procedure 如何使用Spring RestTemplate调用HTTPS restful Web服务 - How to call HTTPS restful web services using Spring RestTemplate 如何将DTO传递到Restful Web服务 - How to Pass the DTO to the Restful web services 使用 jackson REST web 服务(RESTful Z38008DD81C2F4D7985ECF6839D47D60BC4555EE00C3FZ web 服务(RESTful Z38008DD81C2F4D7985ECF)@EV0ZCE8 - Request not processing using jackson REST web services (RESTful Spring Controllers) @PathVariable 如何将输入参数传递给来自Rest Client的Jboss BRMS规则 - How to pass input parameters to a Jboss BRMS Rule from Rest Client RESTful web 服务框架与 Spring MVC REST 支持比较 - RESTful web services framework comparison with Spring MVC REST support 如何使用angularjs服务将表单参数传递给Rest? - How to pass form parameters to Rest using angularjs services? 如何通过Java ReSTful Web服务中的输入参数值来使用户存储过程完成更新和创建记录操作 - How to user stored procedure to fulfill update & create record operations by input parameters values in java ReSTful web services 如何创建带入参的Restful web服务? - How to create a Restful web service with input parameters? 如何使用RESTFul Web服务发送请求? - How to send request, by using RESTFul web services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM