Currently I am working in a project where we are using restful services and jdbc connection. We can handle request response either by jsonObject or with a POJO class. I am confused between these. which one to use and why?
Yes you can read response by using either but if you use jsonObject it will give you an immutable JSON object value. Now it all comes down to your requirement if you have a requirement where you don't need to change any value then you can directly read the response and send it to where ever you want but if you are doing any modification then you need POJO with getter and setter methods.
Similarly in case of creating a request you can use below code:
JsonObject object = Json.createObjectBuilder().build();
but then you end up creating all the nodes and child which is ok for small request but it there are more fields in request then using POJO is a good idea.
POJO:
Its a simple blue print that defines the properties with getter and setter.
jsonObject:
This is a simple data interchangeable format used for client-server interaction
You can use both. If you are receiving Json request or response as string you can convert it to a Json object. Then you can map that fields into POJO class and use them through the POJO class to build your code logic.
Related
Most of the json serializations work using object property accessors like getter and setter methods. I am trying to serialize third party object with no get/set methods(and I don't have control to modify source) to json and send over REST service.
But the final json produced doesn't have all the properties data as like in my object. This is obvious due to no accessor methods.
Is there any other way I can prepare JSON in this scenario?
Otherwise, is there any other way I can send this 3rd party object over my rest service as is without compromising on it's properties values? (I considered like object serialization and send using streams, but that looks like unconventional).
Maybe most. GSON on the other hand uses reflection to directly setup fields. You actually have to force it to not use reflection (see Gson avoid reflection).
So one solution would be to use that library. And just to be precise: gson uses reflection to identify the fields in your bean classes directly, without relying on getters/setters.
I would like to implement a universal Data Model class which will have one attribute of type string and it will contain the entire server json response
So I am a junior android developer and the question i have in mind is that i have to create a different data model class for different server response. so i thought of a workaround and created a single data model for all my view using adapters to inflate data. Now this data model class has only one attribute of type string and it will contain the entire json response i get from my api which i can parse later in my adapters.
I just want to know if my approach is wrong or is it much cleaner practice.
and/or are there any performance benefits?
try creating a generic class like
public Response<T> {
String status;
T data;
String error;
}
And instead of parsing response manually use Gson library to convert your Json to a model class and then pass the model to adapters
In their example (http://square.github.io/retrofit), the third code block appears to retrieve data in the type List<Repo>, but where does the conversion from a string to a JSON array to List<Repo> occur? I'm a bit lost as to how Retrofit works.
In trying to replicate this with my own REST api, (json -> List<User>)...
java.lang.IllegalArgumentException: Could not locate call adapter for java.util.List<com.keenant.app.User>.
Behind the scenes, Retrofit uses Gson to convert the JSON to domain objects. In your case, Gson can't deserialize your User object. You'll probably need to register a custom TypeAdapter so that Gson knows how to handle your User objects.
I am writing a REST API with a Java/Jersey/Jackson stack. All JSON parsing and generating is done with Jackson. The REST layer is handled by Jersey. It will be embedded in a Grizzly container.
In my API, I accept JSON in my endpoints. For example:
#POST
public Response post(final SomeObject input) {
return ...;
}
What is the best way to validate the input? There are certain things I would like to validate:
input must be not null
certain fields of input must be not null
certain fields of input must follow a regular expression (text fields)
certain fields of input must be in a range (numeric fields)
...
If possible, I would like to change my code as less as possible. That is, I prefer to annotate my classes, methods and parameters to integrate the validation.
You can use a JSON Schema.
And since you use Jackson, you can use my library which does exactly that.
However this means you'd need to change your logic so that you receive the JSON (as a JsonNode) instead of the serialized POJO, and only then serialize to your POJO.
You can also BeanValidationApi (javax.validation.constraints) and then annotate your fields with #NotNull,#Pattern, etc. Jersey also provides Bean Validation Support
JSONObject class of package org.codehaus.groovy.grails.web.json does not implement Serializable.
I want to make this object serializable as I am using session replication among application servers and JSONObject gets saved in session.
I have two options to achieve this:
Subclass JSONObjects that implements Serializable.
use toString() method on JSONObject object while saving this object in session.
Can anyone please suggest which one of two options should I use and what is the reason?
I think the toString method is what's intended to be used here. That method already returns the JSON text of the object, which is suitable for transmitting or storing.
You don't need to serialize JSON, it is already in a store-able form(sort of the whole point of JSON). Grails has JSON parsers and "slurpers" all ready to use for you. So just store the JSON data as a string and use a parser on it to read it back out of the file.