NKS
NKS

Reputation: 1160

How to retrieve JSON passed from ajax call to Java API

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.

Upvotes: 0

Views: 632

Answers (4)

0gam
0gam

Reputation: 1423

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.
}

Upvotes: 1

Alexandru Marina
Alexandru Marina

Reputation: 906

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

Upvotes: 0

NKS
NKS

Reputation: 1160

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

Upvotes: 0

Chabukswar
Chabukswar

Reputation: 25

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);
}

Upvotes: 0

Related Questions