1

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.

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 -

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

thanks in advance

2
  • which rest client are you using? and how are you requesting the url? Commented Dec 23, 2015 at 12:59
  • I'm using "WizTool.org RestClient 3.2.2", Commented Dec 23, 2015 at 13:02

1 Answer 1

1

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. Now copy this json format for the user and remove all the above code which was added to your controller.

In wiztool rest client:
1. enter the url "http://localhost:8080/XYZ2/rest/user/userformat". Assuming that you are deleting its corresponding controller mapping after this process
2. select method as GET
3. click the '>>' button to fire the request
4. copy whatever json you get in the HTTP response "Body" as shown in figure at the bottom
wiztool rest client: get user json format

User json might be something like the below one. I have included only few properties. For rest of them you can get from output of the above introduced url "userformat".

{
"userId": 1,
"firstName": "ronaldinho",
"lastName": "Gaucho",
"userMailId": "[email protected]",
"roleId": 2
}

You can use this in request body for this url "http://localhost:8080/XYZ2/rest/user/create"

In wiztool rest client:
1. enter the url ""http://localhost:8080/XYZ2/rest/user/create""
2. select method as POST

enter image description here

  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. Now paste the user json format which we copied from the output of first url and paste it in the text area. you can check with the below image enter image description here
  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. Also it is better to use this way than creating the json manually.

Sign up to request clarification or add additional context in comments.

2 Comments

thnaks for suggestion, can u tell me how to pass parameters from client, like in above snippet i'm passing manually, instead using any annotation or parameter technique, how can i pass those parameters so that method will receive those through Rest-Client body
check my edited answer... if you know the json format of the user object then you can jump to "steps for firing create" in the answer. but your json format in the posted question is incorrect. check the answer for the format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.