Representing Abstract JSON Objects as models in Java - java

Ok so I am making API requests to retrieve certain things like movies, songs, or to ping the server. However all of these responses are contained within the same response JSON object that has varying fields depending on the response. Below are three examples.
ping
{
"response" : {
"status" : "ok",
"version" : "0.9.1"
}
}
getIndexes
{
"response" : {
"status" : "ok",
"version" : "0.9.1",
"indexes" : {
"index" : [ {
"name" : "A",
"movie" : [ {
"id" : "150",
"name" : "A Movie"
}, {
"id" : "2400",
"name" : "Another Movie"
} ]
}, {
"name" : "S",
"movie" : [ {
"id" : "439",
"name" : "Some Movie"
}, {
"id" : "209",
"name" : "Some Movie Part 2"
} ]
} ]
}
}
}
getRandomSongs
{
"response" : {
"status" : "ok"
"version" : "0.9.1"
"randomSongs" : {
"song": [ {
"id" : "72",
"parent" : "58",
"isDir" : false,
"title" : "Letter From Yokosuka",
"album" : "Metaphorical Music",
"artist" : "Nujabes",
"track" : 7,
"year" : 2003,
"genre" : "Hip-Hop",
"coverArt" : "58",
"size" : 20407325,
"contentType" : "audio/flac",
"suffix" : "flac",
"transcodedContentType" : "audio/mpeg",
"transcodedSuffix" : "mp3",
"duration" : 190,
"bitRate" : 858,
"path" : "Nujabes/Metaphorical Music/07 - Letter From Yokosuka.flac",
"isVideo" : false,
"created" : "2015-06-06T01:18:05.000Z",
"albumId" : "2",
"artistId" : "0",
"type" : "music"
}, {
"id" : "3135",
"parent" : "3109",
"isDir" : false,
"title" : "Forty One Mosquitoes Flying In Formation",
"album" : "Tame Impala",
"artist" : "Tame Impala",
"track" : 4,
"year" : 2008,
"genre" : "Rock",
"coverArt" : "3109",
"size" : 10359844,
"contentType" : "audio/mpeg",
"suffix" : "mp3",
"duration" : 258,
"bitRate" : 320,
"path" : "Tame Impala/Tame Impala/04 - Forty One Mosquitoes Flying In Formation.mp3",
"isVideo" : false,
"created" : "2015-06-29T21:50:16.000Z",
"albumId" : "101",
"artistId" : "30",
"type" : "music"
} ]
}
}
}
So basically my question is, how should I structure my model classes to use for parsing these responses? At the moment I have an abstract response object that only contains fields for the status and version. However, by using this approach I will need a response class that extends this abstract class for ever request I make (e.g. AbstractResponse, IndexesResponse, RandomSongsResponse). Also, some models with the same name may have different fields depending on the API request made. I would prefer to avoid making a model class for every possible scenario.
And as an extra note, I am using GSON for JSON serialization/deserialization and Retrofit to communicate with the API.

Related

Paypal Server API. No money transfered

I'm integrating paypal in our shopping system.
I'm using the Java Api.
In the current form the process is the following:
The user chooses paypal as payment and clicks on "Pay Order"
The Server sends a an createOrder Request with Capture Intent.
The Server receives a response with links.
The user is redirected to the "approve" link.
After finishing paypal redirects the user to the shop page with the "thanks for your order message".
All this is working as expected.
But no payment is done / no money transfered.
What am I doing wrong here?
Thanks in advance!
For reference the request/response:
Request: {}, {
"application_context" : {
"user_action" : "PAY_NOW",
"landing_page" : "BILLING",
"return_url" : "https://www.foobar.de.localhost:8443/payment/paypal?result=ok&order_id=MGS063464&secret=2E1C1B304178...",
"brand_name" : "<removed>",
"cancel_url" : "https://www.foobar.de.localhost:8443/payment/paypal?result=cancel&order_id=MGS063464",
"shipping_preference" : "SET_PROVIDED_ADDRESS"
},
"purchase_units" : [ {
"amount" : {
"breakdown" : {
"shipping" : {
"value" : "3",
"currency_code" : "EUR"
},
"item_total" : {
"value" : "2.45",
"currency_code" : "EUR"
}
},
"value" : "5.45",
"currency_code" : "EUR"
},
"reference_id" : "MGS063464",
"shipping" : {
"address" : {
"country_code" : "DE",
"address_line_1" : "<removed>",
"admin_area_2" : "<removed>",
"postal_code" : "<removed>"
},
"name" : {
"full_name" : "<removed>"
}
},
"description" : "<removed>,
"items" : [ {
"quantity" : "1",
"name" : "<removed>",
"unit_amount" : {
"value" : "2.45",
"currency_code" : "EUR"
},
"sku" : "OCI08"
} ]
} ],
"intent" : "CAPTURE"
}
Response: {}, {
"create_time" : "2021-03-14T10:52:46Z",
"purchase_units" : [ {
"payee" : {
"email_address" : "<removed>",
"merchant_id" : "L4EC8HB5DTVSC"
},
"amount" : {
"breakdown" : {
"shipping" : {
"value" : "3.00",
"currency_code" : "EUR"
},
"item_total" : {
"value" : "2.45",
"currency_code" : "EUR"
}
},
"value" : "5.45",
"currency_code" : "EUR"
},
"reference_id" : "MGS063464",
"shipping" : {
"address" : {
"country_code" : "DE",
"address_line_1" : "<removed>",
"admin_area_2" : "<removed>",
"postal_code" : "<removed>"
},
"name" : {
"full_name" : "<removed>"
}
},
"description" : "<removed>",
"items" : [ {
"quantity" : "1",
"name" : "<removed>",
"unit_amount" : {
"value" : "2.45",
"currency_code" : "EUR"
},
"sku" : "OCI08"
} ]
} ],
"links" : [ {
"method" : "GET",
"rel" : "self",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P"
}, {
"method" : "GET",
"rel" : "approve",
"href" : "https://www.sandbox.paypal.com/checkoutnow?token=5MY66978KX626104P"
}, {
"method" : "PATCH",
"rel" : "update",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P"
}, {
"method" : "POST",
"rel" : "capture",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P/capture"
} ],
"id" : "5MY66978KX626104P",
"intent" : "CAPTURE",
"status" : "CREATED"
}
You are missing an API call, step 5 should instead be: The Server sends a Capture Order request.
Your final Step 6 should be to thank the buyer only if the capture was successful
See 'Capture Order' in the documentation.
Redirecting to an "approve" link is an old integration method, for old websites. For a modern user experience you should keep your page loaded (not redirect away) by changing your 'Create Order' and 'Capture Order' to be two server routes that return only JSON data (no other HTML or text)
Pair those routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

How to map two datasets in spark Java

Hi I'm reading data from mongodb into spark application.
My mongodb contains 2 collections.
One is profile_data(actual data with field names)
(Which holds all the input data including some unique fields)
{
"MessageStatus" : 2,
"Origin" : 1,
"_id" : ObjectId("596340fe8b0fa35d2880db1a"),
"accerlation" : 19.4,
"cylinders" : 4,
"displacement" : 119,
"file_id" : ObjectId("59633e48b760e7c8071a6c1c"),
"horsepower" : 82,
"modelyear" : 82,
"modified_date" : ISODate("2017-07-10T08:47:01.641Z"),
"mpg" : 31,
"snet_id" : "new_project",
"unique_id" : "784",
"username" : "chevy s-10",
"weight" : 2720
}
And another collection is : predictive_model_details(Which holds the ML model details like model name, feature fields and prediction field just like metadata)
{
"_id" : ObjectId("56b4351be4b064bb19a90324"),
"algorithm_id" : "55d717a53d9e22022ff2a1e9",
"algorithm_name" : "K- Nearest Neighbours (IBK)",
"client_id" : "562e1d51b760d0e408151b91",
"feature_fields" : [
{
"name" : "Origin",
"type" : "int"
},
{
"name" : "accerlation",
"type" : "Double"
},
{
"name" : "displacement",
"type" : "Int"
},
{
"name" : "horsepower",
"type" : "Int"
},
{
"name" : "modelyear",
"type" : "Int"
}
],
,
"makeActiveStatus" : "0",
"model_name" : "test1",
"parameter_type" : "system_defined",
"parameters" : [
{
"symbol" : "-K",
"value" : "1"
}
],
"predictor" : {
"name" : "mpg"
"type" : "Int"
},
"result_exists" : true,
"snet_id" : "new_project"
}
So I've created 2 datasets in spark for two collections in MongoDB. Now I want to map these 2 Datasets with all feature fields together and prediction field together.
And common field in 2 datasets is snet_id.
Could anyone please help?

STS Custom Template

I'm having an issue in STS creating a custom template. What I want is the ability to do token-based replacement within all files in the project. I've read this is possible with the "token" replacement, but cannot seem to get the wizard.json to let me accept multiple inputs. The "example" you see everywhere is replacing the top-level-package, which I've got to work great.
Anyone have any examples of doing multiple inputs in the wizard.json, or have any idea on what I'm doing wrong?
Here is what I have, which doesn't work:
{
"info" : {
"elements" : {
"element" : [
{
"name" : "mysystem",
"description" : "Please the name of your system (ex. marketing, hr, accounting, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
],
"element" : [
{
"name" : "myapp",
"description" : "Please the name of your application (ex. thisapplication, thatapplication, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
]
},
"projectName" : "projectName",
"pages" : {
"page" : [
{
"order" : 0,
"description" : "System Info"
}
]
}
}
}
The "myapp" picks up and does the translation, but the "mysystem" never even gets displayed for input (screen one is just the myapp text).
For those who run into the same problem, I have figured this out. Here is what I ended up with.
{
"info" : {
"elements" : {
"element" : [
{
"name" : "mysystem",
"description" : "Please the name of your system (ex. marketing, hr, accounting, etc).",
"type" : "java.lang.String",
"page" : 0,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
},
{
"name" : "myapp",
"description" : "Please the name of your application (ex. thisapplication, thatapplication, etc).",
"type" : "java.lang.String",
"page" : 1,
"required" : true,
"pattern" : "\\w+",
"replaceKind" : "token"
}
]
},
"projectName" : "projectName",
"pages" : {
"page" : [
{
"order" : 0,
"description" : "System Info"
}
],
"page" : [
{
"order" : 1,
"description" : "System Info 2"
}
]
}
}
}

Customize the PrettyPrint options in Jackson?

I'm aware of the writerWithDefaultPrettyPrinter option in Jackson, but is there any way to customize it? See examples below.
If this isn't possible in Jackson, if you can't change pretty print options, then is there another popular JSON library that would do it?
Summary of options to change:
Don't open multiple containers on the same line
Don't close and open containers on the same line
Use 4 spaces as indents instead of 2
(another option, though I wouldn't use it) Open containers on a new line so that they line up vertically with their closing marker
Example of what it outputs now:
[ {
"id" : "12",
"payload" : [ {
"name" : "url",
"value" : [ {
"name" : "url",
"value" : "http://foobar.com"
} ]
}, {
"name" : "tags",
"value" : [ {
"name" : "tags",
"value" : "red"
}, {
"name" : "tags",
"value" : "green"
}, {
"name" : "tags",
"value" : "blue"
}, {
...
Example of what I'd like to get:
[
{
"id" : "12",
"payload" : [
{
"name" : "url",
"value" : [
{
"name" : "url",
"value" : "http://foobar.com"
}
]
},
{
"name" : "tags",
"value" : [
{
"name" : "tags",
"value" : "red"
},
{
"name" : "tags",
"value" : "green"
},
{
"name" : "tags",
"value" : "blue"
},
{
...

Is it possible to add list of objects into embedded array document in mongo document?

I am having the mongo document as below:
{
"_id" : ObjectId("506e9e54a4e8f51423679428"),
"description" : "ffffffffffffffff",
"menus" : [
{
"_id" : ObjectId("506e9e5aa4e8f51423679429"),
"description" : "ffffffffffffffffffff",
"items" : [
{
"name" : "xcvxc",
"description" : "vxvxcvxc",
"text" : "vxcvxcvx",
"menuKey" : "0",
"onSelect" : "1",
"_id" : ObjectId("506e9f07a4e8f5142367942f")
} ,
{
"name" : "abcd",
"description" : "qqq",
"text" : "qqq",
"menuKey" : "0",
"onSelect" : "3",
"_id" : ObjectId("507e9f07a4e8f5142367942f")
}
]
}
]
}
Now i want to change this to :
{
"_id" : ObjectId("506e9e54a4e8f51423679428"),
"description" : "ffffffffffffffff",
"menus" : [
{
"_id" : ObjectId("506e9e5aa4e8f51423679429"),
"description" : "ffffffffffffffffffff",
"items" : {
{
"name" : "xcvxc",
"description" : "vxvxcvxc",
"text" : "vxcvxcvx",
"menuKey" : "0",
"onSelect" : "1",
"_id" : ObjectId("506e9f07a4e8f5142367942f")
} ,
{
"name" : "abcd",
"description" : "qqq",
"text" : "qqq",
"menuKey" : "0",
"onSelect" : "3",
"_id" : ObjectId("507e9f07a4e8f5142367942f")
}
}
}
]
}
Is this possible in mongo? In the first schema, updating is not possible atomically becoz we can't use two "$" while updating deep layer. So i thought to change schema as same as second one, how can i achieve it?
For first one i have used "$push" for adding items inside menus...
Any help would be great..
Your update is changing 'menu' object, so I would suggest changing the schema so that the menu is the top level document, rather than an array in another document.
Menu could either have a field referencing the top level object (in another collection) that it belongs to, or you can denormalize the fields of the top level object into each menu document.
Without knowing complete requirements of the application, it's difficult to know when the schema is "good enough".

Categories

Resources