Passing POJOs vs Strings (json encoded) to my applications WebApi - java

I have some forms and databases that will fill up some POJO's with data. Using Json (gson) I"m going to upload the data to my web server. The server will send back a responses that will eventually be placed back in objects.
My question is, should I encode/decode my objects to json and then pass strings back and forth to my WebApi class? Or should I pass my WebApi class my object and have it pass back an appropriate response object (based on the method I call.)?
So at when I call my Api that has all the http conection stuff in it, it would look something like this.
myWebApi postSomeData( jsonData ); //
Should jsonData be a pojo that will be encoded into json string inside myWebApi or should I encode it into json and pass in a string.
Or in otherwords
String postSomeData( String jsonData){
web code here..
}
or
ResponsePojo postSomeData( PostData myData){
...
myMapper.MapFrom(myData); //converts pojo to json
...
webcode
}
At some point there will be images included in the data needed to be uploaded.

I would recommend using json for data transfer, it will make your services technology independent.
You can use flexjon to serialize java objects in a pretty direct way on the server side.
For images you will need to do post http POST anyway, this should be done separately from data submit since an error in a image upload should not result in data loss. Typically you'll associate the id of your data object in the uploaded image (or reversly), so you need to insert your object to db and wait for the response to insert the dependency id to your image.
Best Regards,
Zied Hamdi
http://1vu.fr

Related

how to retreive data(present in JSON or anyother format) from a url in springboot?

I want to hit a URL which contains data, i want to retreive this data using spring boot and convert it to AVRO format. data needs to be picked up in JSON format
data and url in one sentence raises alarm. Unless you have a particular need (usually legacy reasons) to use URL to send data, use requestbody for sending the data.
Remember, many browsers will have URL character limit even though http protocol doesn't talk about any
If you are adamant about sending Jsondata using url, either look into base64encode it and send and then base64decode it on serverside.
Or do URL encoding and Decoding . Because there is a highchance that the data might not be URL-safe

Rest: filering on Json response to save bandwidth

I have a rest service, not restfull.
The module is stateless and just expose some rest endopoints: receives a request, transform it, invokes external endopints, and then compose a Json response.
The thing is that the response is quite verbose, and some of the fields are quite big.
I've been asked to provide a filtering at request level, so that the response contains only the requested data.
Just to provide an example: my exposed method is a POST, and accept an image encoded BASE64, and the response too, can contain an image, and many other fields:
request: { image="<here a long BASE64 encoded request>"}
response: {result="success",
message="it was a success",
responseImage="<another base64 encoded image>",
longArray1=[
{first="...", second="...", third="..."},
{first="...", second="...", third="..."},
....many!
],
longArray2=[
{first2="...", second2="...", third2="..."},
{first2="...", second2="...", third2="..."},
....many!
]
}
What I want is avoid to reinvent the wheel. Is there a known pattern to make a query (adding something in the queryString, or in the Json request, and how) so that the consumer can choose in advance what fields to receive in response, and save bandwidth in the response?
The idea to have a Filter class is a good one. Your Filter class should provide fields which mirror the response object and use defaults for fields which are not provided. E.g. if a user of your api does not pass any json serialized filter object in their request, they should still receive data with the default filters. Extending your example, the request would look like
request: {
image="<here a long BASE64 encoded request>",
requestFilters:{
responseImage="<another base64 encoded image>",
longArray1={ firstFilter:"PASSED_ONLY",
secondFilter:"FAILED_ONLY",
....many!
},
longArray2={ // using defaults for firstFilter and secondFilter
thirdFilter="PASSED",
fourthFilter="FAILED",
....many!
}
......
}
}
Hope that helps.

How can I send an Array as a parameter for making body for requesting to the server?

I have to send some parameters for making body for requesting to the server.
like this:
"UserName":"a.m",
"CityId":"1",
"UserCategories":
[
{"CategoryId":"ab2d948a-59d1-420d-a29b-8fd88c2d637c"},
{"CategoryId":"237b6334-3c1f-44ac-bd87-a8e6be0b2144"}
]
I don't know how can I send UserCategories as an array.because it is not String that I can send it like other parameters for making body.
I should have the body like this for getting response:
body: {"CityId":"1","UserName":"a.m","UserCategories":[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]}
but now my body is:
body: {"CityId":"1","UserName":"a.m","UserCategories":"[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"}
I'm really confused!
When you send or receive JSON data, it is always as a string. We might speak of JSON objects or JSON arrays, but they are really string representations of objects and arrays. You can send the data exactly as you have written here as long as you enclose the entire thing in {}. You can send just the array in string form if you wish, exactly as you typed it here. The sender should create a string from the array and the receiver should parse the JSON string into an array. There are many libraries available to help you do this in every language.
your existing UserCategories object is List/Array format after that changed in String format so only its happen
"UserCategories":"[
{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},
{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"
this is your second-time Json, Here see your List sent with ""
like "UserCategories":"[]"
Yes. You can do that by using retrofit library. You can sent object,arraylist using Retrofit.
https://futurestud.io/tutorials/retrofit-send-objects-in-request-body.Check this url to sent Object. You can also sent arraylist under Object In body

How to deserialize JSON object on the client side

I'm using Netty Socket IO to send socket.io content from Java (well, actually Scala.) I've tried several ways of sending a JSON object to the client:
hardcoded String
standard Java json library
Gson library
Each time, when the data gets to the client, it's just a string that looks like a JSON object. I'm currently sending it with:
case class Data(message:String) and gson.toJson(new Data("what up")). The object looks like this on the client: "{"message":"what up"}", although it's just a String and not a JSON object.
Any ideas on how to get an actual JSON object on the other side?
Thanks
This works (JQuery):
$.parseJSON(string)

How to parse serialized json from php web service in java play framework 1.2.x

I need to use some from from a php web service which rendering its data by serializing json in java play framework 1.2.x. What i am doing just using play WS function. and i am getting data from that service. But when I try to get it with JSONObject it throws excepiton which is so normal, because the returned data does not look a json format well. Any body who knows any workarounds or solution would be appreciated.
HttpResponse htp = WS.url("http://www.geoplugin.net/php.gp?ip=78.171.90.49").get();
System.out.println(htp.getContentType()+"\n"+htp.getStatusText()+"\n"+htp.getString());
The returned data :
a:18:{s:17:"geoplugin_request";s:12:"78.171.90.49";s:16:"geoplugin_status";i:200;s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.";s:14:"geoplugin_city";s:8:"Istanbul";s:16:"geoplugin_region";s:8:"Istanbul";s:18:"geoplugin_areaCode";s:1:"0";s:17:"geoplugin_dmaCode";s:1:"0";s:21:"geoplugin_countryCode";s:2:"TR";s:21:"geoplugin_countryName";s:6:"Turkey";s:23:"geoplugin_continentCode";s:2:"EU";s:18:"geoplugin_latitude";s:7:"41.0186";s:19:"geoplugin_longitude";s:9:"28.964701";s:20:"geoplugin_regionCode";s:2:"34";s:20:"geoplugin_regionName";s:8:"Istanbul";s:22:"geoplugin_currencyCode";s:3:"TRY";s:24:"geoplugin_currencySymbol";s:15:"YTL";s:29:"geoplugin_currencySymbol_UTF8";s:3:"YTL";s:27:"geoplugin_currencyConverter";s:6:"2.2669";}
You are accessing the PHP endpoint. You need to hit this URL instead:
http://www.geoplugin.net/json.gp?ip=78.171.90.49

Categories