Question

I have a successfully working SmartGWT 4.0 and Spring MVC application. I have DataSources defined and they call the correct RESTful web-services and return JSON.

The issue that I have is that if I pass in an ID for a user to my RESTful web-services, it works. The single User object is returned to me:

{data: "username":tholmes,"password":"mypwd",
"other":{"field1":"data1","field1":"data1","field1":"data1"}}

I can then use JSOHelper to get attributes, delete attributes, and add new attributes. I believe this is what transformResponse was meant to do.

So now, if I get a list of all my users, then from the same RESTful web-service, I get an ArrayList of User objects, and the JSON data now looks like:

[
     {data: "username":tholmes,"password":"mypwd",
        "other":{"field1":"data1","field1":"data1","field1":"data1"}},
     {data: "username":tholmes,"password":"mypwd",
        "other":{"field1":"data1","field1":"data1","field1":"data1"}},
     {data: "username":tholmes,"password":"mypwd",
        "other":{"field1":"data1","field1":"data1","field1":"data1"}},
]

So, the code that I had before to change the JSON was working, with a single item. And now when I have multiple items, the code does not work. What I need to find out, is how this is best solved.

I think at one point everything was returned as a collection of objects into a SmartGWT datasource, but maybe that isn't the case anymore.

I might have to check the number of rows, and if there is more than 1, then iterate through all the data that is brought back.

If there are any other easier, more mainstream solutions, that would be great.

Thanks!

Was it helpful?

Solution 2

I was hoping there would be a quick answer, very specifically using the JSOHelper and within the SmartGWT datasource. After some thought ... I realized that the Object being brought back from the RESTful webv-service has been interated through once and created the JSON.

Within the RESTful web-service, you could have an ArrayList with hundreds if not thousands of records. To iterate through all this within a SmartGWT datasource, using JSOHelper doesn't seem very efficient.

I was thinking ... how can I change this on the back-end without affecting the back-end. In other emails, it talked about building a custom serializer/deserializer for helping to convert your object to JSON.

Then I found the @JSonProperty from the XML Jackson mapper. This is within our project which features our entities and dao. We had previously added the latest Jackson 2 Fast Mapper to this project on the back-end. So, on the entity class, we can have:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("sommeOtherId")
private long id;

Then I don't have to do anything in the SmartGWT Datasource, the JSON comes down exactly as I want it, with the names of the fields exactly as I want.

I hope this helps someone else.

OTHER TIPS

Your both JSON are not a valid JSON.

Please check it on The JSON Validator .

Here is the valid JSON for first one.

{
    "username": "tholmes",
    "password": "mypwd",
    "other": {
        "field1": "data1"
    }
}

Here is the valid JSON for second one.

[
    {
        "username": "tholmes",
        "password": "mypwd",
        "other": {
            "field1": "data1"
        }
    },
    {
        "username": "tholmes",
        "password": "mypwd",
        "other": {
            "field1": "data1"
        }
    },
    {
        "username": "tholmes",
        "password": "mypwd",
        "other": {
            "field1": "data1"
        }
    }
]

Try this sample code:

StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/abc1.txt")));
String line = null;
while ((line = reader.readLine()) != null) {
    builder.append(line);
}
reader.close();

Gson gson = new Gson();
UserDetails[] userDetails = gson.fromJson(builder.toString(), UserDetails[].class);
System.out.println(userDetails.length);

...

class UserDetails {
    public String username;
    public String password;
    public Map<String, String> other;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Map<String, String> getOther() {
        return other;
    }

    public void setOther(Map<String, String> other) {
        this.other = other;
    }

}

Note: text file contains second JSON string as shown above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top