I am using RESTful API to retrieve some data over HTTP using retrofit2.
I have the following type of data, and I would like to store replies as object:
The problem is, replies object is an empty string when no values (or fields) are present, i.e:
replies = ""
When there are fields, then replies object is given as:
replies = {
"kind" : "Listing",
"data" : {
"key" : "data",
"value" : {
"modhash" : "",
.
.
.
}
}
}
What is bothering me is the inconsistency between data types of replies, which is given as an object when nonempty and when empty, given as a String ("").
My dilemma is this: since Java is a statically typed language, I need to define what replies is beforehand, but I cannot define it as neither Replies nor String because of its inconsistency. How can I resolve this issue?
You need to change answer from your api, when replies is an empty you should send replies : {}
Related
I have to set a literal JSON value to define the home directory mapping of a user in Java based CDK definition:
CfnUser user1 = CfnUser.Builder.create(this, "user1 ")
.userName("user1")
.serverId(transferServer.getAttrServerId())
.homeDirectoryType("PATH")
.homeDirectory("/path/to/home")
.homeDirectoryMappings(List.of("Entry:/", "Target:/path/to/home"))
.role(role.getRoleArn())
.build();
The above code fails, because the List passed to homeDirectoryMappings() is not a software.amazon.awscdk.IResolvable. Somehow I have to create a IResolvable which represents this JSON:
[
{
"Entry":"/",
"Target":"/bucket3/customized-reports/"
}
]
I searched throuth the implementations of IResolvable but cannot find a suitable class.
So how can I pass a JSON literal to a method that takes a IResolvable?
After reading the error message one more time I noticed, that the homeDirectoryMappings() method accepts a List of HomeDirectoryMapEntryProperty instances. So there's actually no need for a IResolvable.
So the correct code lookes like this:
CfnUser user1 = CfnUser.Builder.create(this, "user1 ")
.userName("user1 ")
.serverId(transferServer.getAttrServerId())
.homeDirectoryType("LOGICAL")
.homeDirectoryMappings(List.of(new HomeDirectoryMapEntryProperty.Builder()
.entry("/")
.target("/s3-bucket/home/${transfer:UserName}")
.build())
)
.role(dlRole.getRoleArn())
.build();
I am working on the Stock and Exchange Markets. I have a situation like : I need to take a string from the log and convert it to "Message" type Object. As per this link I have tried using all the three methods of the "MessageUtils" class in JAVA. But my String is being stripped to a Message class type object with unique tags. But as my string is "MarketDataIncrementalRefresh" type I want each every tag to be present in the Message.
For example : I am providing the following string to "MessageUtils.parse()" method.
8=FIX.4.2|9=00795|35=W|49=TT_PRICE|56=SAP0094X|34=2392|52=20170623-04:41:33.375|55=CL|48=00A0HR00CLZ|10455=CLQ7|167=FUT|207=CME|15=USD|262=MDRQ-751|200=201708|18210=1|387=12292|268=24|269=0|290=1|270=4290|271=33|269=0|290=2|270=4289|271=34|269=0|290=3|270=4288|271=40|269=0|290=4|270=4287|271=38|269=0|290=5|270=4286|271=46|269=0|290=6|270=4285|271=53|269=0|290=7|270=4284|271=46|269=0|290=8|270=4283|271=66|269=0|290=9|270=4282|271=48|269=0|290=10|270=4281|271=64|269=1|290=1|270=4291|271=21|269=1|290=2|270=4292|271=40|269=1|290=3|270=4293|271=48|269=1|290=4|270=4294|271=83|269=1|290=5|270=4295|271=62|269=1|290=6|270=4296|271=46|269=1|290=7|270=4297|271=34|269=1|290=8|270=4298|271=55|269=1|290=9|270=4299|271=31|269=1|290=10|270=4300|271=128|269=2|270=4291|271=1|269=4|270=4280|269=7|270=4292|269=8|270=4277|10=044|
But what I am getting is this:
8=FIX.4.2|9=192|35=W|34=2|49=TT_PRICE|52=20170622-14:16:23.685|56=SAP0094X|15=USD|48=00A0HR00GCZ|55=GC|167=FUT|200=201708|207=CME|262=MDRQ-21|268=25|269=0|270=12510|271=24|290=1|387=121890|10455=GCQ7|18210=1|10=036|
As you can observe only unique tags are present in the String. But I want each and every tag , no matter how many times it exists in the provided string.
Please can anyone help me doing this in JAVA. It will be really appreciable.
Below is the code I am using for converting :
MessageUtils mu = new MessageUtils();
Session session = Session.lookupSession(sessionID);
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
// Message msg = new Message(str, false); //This can also be used for converting
System.out.println(msg.toString());
The other thread says:
MessageUtils.parse(MessageFactory messageFactory, DataDictionary dataDictionary, java.lang.String messageString)
And your code says:
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
So you need to fix your data dictionary and pass it to the parse method instead of passing 'null'
I think the problem is as follows. There's a repeating group that starts with tag 286 (NoMDEntries). The order of fields in a repeating group should be strict, i.e. the same order as the definition of the repeating group. See Market Data - Snapshot/Full Refresh or the data dictionnary supplied by QuickFIX/J (FIX42.xml).
The 268 tag should be followed by 269 and then 270. I am seeing in your message string: |268=24|269=0|290=1|270=4290| which is the incorrect order of tags. That is probably the reason why the message is truncated by MessageUtils.parse.
As a test you could try to manually correct the order in the string and try parsing that to see if that gives the correct message.
I am a new guy to Java and to the REST API .I would like to know the way of receiving the dynamic data from client by REST api in java and process it.
For example,
Some times client will send the data like below,
{
"User" : "XXXX",
"Role" : "ZZZZ",
"Product" :
{
"Name" : "yyyy",
"Valid to" : "04/4/2025",
"Licensed version" : "jjjjj",
},
}
In the next contract, client may send like below,
{
"User" : "XXXX",
"Role" : "ZZZZ",
"Product" :
{
"Name" : "yyyy",
"Expiry Date" : "04/4/2025",
"Activation Date" : "jjjjj",
},
}
By referring the both examples, "Product" section having different data .For additional information may Client can send the additional data in this "Product" section. Would it be possible to make my REST Api to receive this type of dynamic data?.
If possible please let me know how can my REST api will able to receive this type of dynamic data and process it?.
Thanks
I do the same in php with the slim network and Doctrine 2. In Doctrine 2 i defined the entities with all the possible fields. In my javascript fronted, i collect the dynamic data and then deserialize it into the entityclasses. The classes can get saved into the database then or you can do whatever you need to do to process the data.
http://www.restapitutorial.com/ is a very good tutorial on how to design your REST API, i suggest reading it.
ya i know that it's very usual problem while mapping but my problem is some different hear is the scenario
when my response have the data it gives me JSON Response like this
{
"responseID": "110",
"resultSet": [
{
"USERNAME": "Aninja",
"position": "Developer",
"salary": "60000"
}
],
"isSuccessful": true,
"rtnCode": "0000"
}
and below is the same JSON response when data is not found
{
"responseID": "123",
"resultSet": {},
"isSuccessful": true,
"rtnCode": " "
}
as i can see hear when response have some data result set have JSON Array but when no data found we have JSON Object as a response
so this is the reason I'm getting this problem.
so my question is that how should i handle this problem thanks for your response
Edit: the main problem is that i have made my model like list of JSON Object it works fine when there is result but it gives me error Can't convert JSON Object to JSON Array when result is empty s please suggest me how can i hanle it I'm using Jackson 2.2 i have also tried #JsonInclude(Include.NON_EMPTY) and #JsonInclude(Include.NON_NULL)
I wouldn't say it is mistake from server or back-end. But it is always a good practice to provide appropriate "Null Object Pattern" which describes the uses of such objects and their behavior.
So for better practice array which doesn't have any values should be sent back using "[]". So in this case "resultSet" should be given as [] instead of {} so it can be easily understood at front-end.
There are number of examples here which shows why it is useful to follow Null Object Pattern.
For example, if you are returning count in you response and there is no count then it is better to use "0" instead of "null".
I'm using java GAE server. I store List on my entity (as strings are very limited in length in GAE). I send Map to the client through the Endpoint, and I put this list under some key. Then I retrieve this list on Android client - and I get classcast exception. It appears that HashMap< String, Object > sent from GAE server is seen as JsonMap on Client. Whatever. I proceed, I retrieve my List... and how surprised I was to find out that on the client I got List< ArrayMap >, and on this ArrayMap, my Text is under the key named "value".
There is even more. Under one of the keys in the JsonMap, I had a null value. I retrieve it... and it appears as Object (which is not null). Calling toString on this object gives me some crappy string...
Could anyone tell me why these things are happening? Sure, I can just accept how it, but its strange and not logical, and undocumented... Why my List< Text > magically converts into List< ArrayMap >? How likely is that it varies with, lets say, Android version, or, I don't know, with weather outdoor?... Anyone could help me understand these situations? Or point me some relevant documentation / articles?
Example server-side:
#ApiMethod(name = "retrievePlayer")
public Map<String, Object> retrievePlayer(Map<String, Object> data, User user) throws Exception, OAuthRequestException, IOException {
Map<String, Object> result = new HashMap<String, Object>();
List<Text> list = new ArrayList<Text>();
list.add(new Text("something"));
result.put("myList", list);
result.put("myNull", null);
return result;
}
On the client side, the "result" is of type JsonMap. The "myList" is of type ArrayList (ok). myList.get(0) is of type ArrayMap, and its one-element ArrayMap - the element inside this map has key named "value", and a value of "something". The "myNull" is of type Object and is not null, its toString() method shows something like [Ljava.lang.Object;#1db9742.
I resolved the issues by returning empty string instead of null. For the List< Text >, I iterate through it on and add all the Texts as Strings to new List< String >, and then return this new list (but it costs cpu usage on the server)... I thought it will work more predictably and out-of-the-box.
In this particular example, the method will return an instance of Map<< String, Object>>. If you try to call this endpoint using https://your_app_id.appspot.com/_ah/api/explorer, the json return will be
{ "myList" : { { "value": "something"} }, "myNull" : null }.
The returned json seems to be correct and a null is returned for "myNull". The reason you are getting an instance of Object and not null is because of the handling of JSON null by the JSON library. Please check this link for more explanation (JSON null section).
As for why the List<< Text>> magically converts into List<< ArrayMap>>, this is because you define the return as an instance of Map<< String, Object>> and the returned json does not contain any type information. I think when converting to client objects the type information is obtained from the generated client code which is based on the signature of the ApiMethod.