0

I am making a call to JAVA API as follows:

var userDetails = {
            userId: userId,
            first : "1 one",
            second : "2 two"
        }
    $.ajax({
        type : 'POST',
        url : "http://" + config.domain + config.root + "/myExp/allExperiment",
        dataType : "json",
        data : userDetails, 
        success : function(data) {})
   });

And trying to get the passed object as follows:

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
    public JsonMapModel getAllDatasets(@RequestBody String userDetails) {
         System.out.println("Data is " + userDetails);
}

I am getting following at the API Data is second=2+two&userId=16&first=1+one

Any idea how can I convert the above response to the JSONObject or any other collection so that I can refer the passed JSON appropriately.

2
  • What library are you using on the Java side to process the request? Commented Jul 12, 2016 at 5:28
  • No library used as of now, it just a Spring based API, like to know what should I use/do to parse the requestbody Commented Jul 12, 2016 at 5:32

4 Answers 4

1

You use jackson library. Jackson Convert Java Object to / from JSON

(pom.xml)

<!-- Jackson -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>

(js)

    var userDetails = {
            userId: userId,
            first : "1 one",
            second : "2 two"
        }

    userDetails = JSON.stringify(userDetails);       

    $.ajax({
        type : 'POST',
        url : "http://" + config.domain + config.root + "/myExp/allExperiment",
        contentType : 'application/json',
        data : userDetails,

        success : function(data) {

    },
    error : function(request, status, error) {

    }
});

(Model)

public class TestModel {

    private String userId;
    private String first;
    private String second;

    //getter, setter 
}

(Controller)

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
public @ResponseBody String getAllDatasets(@RequestBody TestModel userDetails) {


    return null; // break point, check model.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Rather than including multiple jackson libraries, I think google-gson is enough for the job. Also I think it should be data : JSON.stringify(userDetails) in ajax call.
oh. gson well. i think to nice your comment. ok. edit. Thank you!
0

You can use Jersey API for conversion of JSON to Value Object at server side. Jersey Jar does automatic conversion of JSON to VO and VO to JSON. Below is the snap shot how you can receive VO at server side:

@POST
@Path("/allExperiment")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public JsonMapModel  getAllDatasets(UserDetails userDetails) {
     System.out.println("Data is " + userDetails);
}

Comments

0

Thanks for the response, as per the suggestion by @Alexandru Marina, following worked for me:

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST)
    public JsonMapModel getAllDatasets(@RequestBody MultiValueMap<String, String> userDetails) {
System.out.println("User id is "  userDetails.getFirst("userId")); }

Thanks Once Again

Comments

0

It should work if you replace String userDetails with a real object for example, like UserDetails userDetails).

Or try to use a MultiValueMap instead of String. From the link below it seems that if this is the parameter than Spring automatically uses a FormHttpMessageConverter. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody

3 Comments

Yes it can be done that way, but I want to have a mechanism where in I can maintain a server side object irrespective of number of parameters in the JSON, for example, if passed JSON have 2 key value pair , or it can be 5 key value pair, that should get stored in a generic collection object at the server
Try to use a MultiValueMap<String, String> instead of String. From the link below it seems that if this is the parameter than Spring automatically uses a FormHttpMessageConverter. docs.spring.io/spring/docs/current/spring-framework-reference/…
Yes it worked for me by using MultiValueMap<String, String>

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.