I am writing a RESTful web service using Java which should be able to take as (POST) input an unknown number of parameter name, value pairs. I am thinking what is the best way to achieve this.
The POST input will be JSON with the expected format being:
{"input" :
{ "x" = 1, "y"="Hello, "z"="1.2.3.4",... }
}
The parameter values are expected to be a mix of int/long/float/string etc. The expected behavior is to save this input to the DB and provide it back on request. The tricky part is with the parameter names being unknown, I cannot write getter and setter methods. Is there a simple way for handling this?
Any pointers in the right direction will be very much appreciated. Can I read this as a List? Do I need to write Custom MessageBodyReaders? Is there some other way of achieving this? The only option I can think of is to have these sent as a file and parse it but am hoping that there are simpler/cleaner solutions. Thank you for any pointers...
If all you need to do is save it and make it available on request, why parse it at all? Simply write the json as a string to your database, and return it as a string while setting the response type to "application/json". The one caveat being you're going to have to have some way to identify which json string you want and associate a unique id that you're going to be able to determine when you're requesting the specific json string in the future.
Something worth noting here would be that when you don't parse something, you don't actually know what it is. Even though a lot of modern libraries/frameworks sanitize against sql injection/xss already, it would be worth it to verify that this is being done in your server if you don't already know for sure.
That being said, if you're still feeling as though you need to parse the json, I would look into gson: http://code.google.com/p/google-gson/
Related
I am facing a certain problem for which i do have some ways to solve, however i am interested if there are more optimal ways to do it. This is a real world problem, but for the purpose of this question i will simplify it.
I have an endpoint called /initialise-string which will return a randomly generated string to the front-end. In the front-end, this string might or might not be changed, however it will be sent to another endpoint on the back-end called /check-if-same-string which will get the string and it needs to check if it is the same string that was previously returned from /initialise-string. So, this would be:
GET www.myapp.com/initialise-string (front-end gets asd-123)
front-end changes string to asd-555
POST www.myapp.com/check-if-same-string (should check if asd-555 === asd-123)
Since this question is for my educational purposes as well, if you can, please provide me answers assuming both if the app is using cookies and if it not using cookies, how you would solve it in each case. Saving to a DB is off limits. Sending both the old and new string back is off limits.
I am looking forward to each answer! Let me know if my question is not clear enough.
Thank you!
To check the String again we need to somehow store the randomly generated string. We can use cookies for this and if we do not want to use DB and we do not want to send the strings to the user then probably we can do it by using some in-memory DB or caching mechanism, so we can store them temporary unless its been verified and also we can apply some time constraint to delete the string from cache after some time. we have to maintain a record to track down the string for the specific request or user to verify it later.
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.
There's a lot of info out there for a newbie like myself when it comes to webservices, I acknowledge that.
However, most of the Google results I've seen tend to be focusing on a specific format or strategy, and all of them different to each other.
As a newbie, I am looking to get more of an overview of the various options open to me, their pros & cons... before I start committing to a specific one.
For example, I have an existing webservice created from a SQL Server source via ASP.net and this by default comes out as a DiffGram. So there are articles that tell me how to parse the diffgram data coming into my Android app, but I still do not know if the diffgram is the best option I should be using in the first place.
Should I, for example be changing my webservice to output JSON ?
Does anyone know of a 101 level tutorial or explanation out there ?
Many Thanks
DiffGram is just another type of XML format. In order to parse the XML data in Android, simply use the XMLFullParse class.
FYI: http://developer.android.com/training/basics/network-ops/xml.html
However if you just want to create a simple data exchange WebService, Json is definitely a better choice. Json is simple, lightweight, easier-to-parse and surely easier to use.
Example:
JsonObject json = new JsonObject(jsonString);
json.put("name", "Michael");
json.put("age", "18");
String name = json.opt("name");
I'm new to Java. I want to send an array (ArrayList) of objects over the network via Java Web Service to my Silverlight app. This ArrayList contains custom class objects:
ArrayList<SVNSearchResult> results
so I'm thinking the best way is to serialize this to an XML String and on the Silverlight part, use LinQ to parse it. If there's a better way to send it please let me know. Thanks.
XML is a good fit for this. JSON would be one of the other usual suspects these days.
Whatever format you end up choosing, make sure you get the encoding right.
For a starter, try JSON. It has a network-efficient format, and is supported by any major language in the world.
XML is only my second choice as it is more complicated to generate/parse and is more verbose.