Using Spring have multiple JSON request (POST) in a single HTTP call - json

How can I use Spring3 framework when combining 2 different REST (using JSON) requests within a single HTTP call? I am using Spring with jackson for JSON conversion. What is the most optimal way of doing it? I am not sure, if it comes under multi-part request.
For example, if I had the following 2 unrelated JSON payloads that I want to combine in a single HTTP call.
"json1":
{
"Name": "abc",
"Age": "111"
}
"json2":
{
"stockName": "xyz",
"stockSymbol": "SS"
}
Once my main controller method handles the initial call, is it then possible to map different additional controller methods for different JSON payloads? I want to understand what is the optimal way of handling such scenarios?
Thanks for the help.

You have to create separate resources and call separately, that is the best approach. In the calling place you can combine both JSON output. Right now you are trying like to have a method with two different different return types

Related

nifi invokehttp post complex json

I trying to use InvokeHttpProcessor in Apache NiFi to perform POST request with complex JSON body.
Accordingly this tutorial: http://www.tomaszezula.com/2016/10/30/nifi-and-http-post-configuration
I know how to use UpdateAttribute processor to add name/value pairs and then apply an additional transformation via AttributesToJSON.
But how to deal with complex JSON?
For example I have to perform request to GoogleAnalytics reporting API, so I need to perform this request:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dateRanges": [{"startDate": "2014-11-01", "endDate": "2014-11-30"}],
"metrics": [{"expression": "ga:users"}]
}
]
}
any ideas?
You can use the GenerateFlowFile and ReplaceText processors to provide a template as the flowfile content and then populate the actual values. Once that JSON object is formed as flowfile content, it should be easy to send it via POST using InvokeHTTP

How to handle 'intermediate' JSON rest service parameters in Spring?

I'm new to Spring development and am currently looking for best practices for creating objects based on JSON data passed to a rest service, which are then to be stored using JPA.
As an example, let's assume a simplified file upload service where clients can post the following JSON object to:
{"filename":"myfile.pdf", "data":someByteArray, "extension":"pdf"}
On the server, I don't want to store the file's raw data in the database, but upload it to some cloud storage and just store a link to it. So once the "data" parameter content is stored and the link is received, it is obsolete.
The options I've come up with is:
a) define one 'union' class to deserialize the request body that comprises all fields ("filename", "link", "extension" AND "data"), marking 'intermediate' data as #Transient, so it won't be stored in the database,
b) define two classes: one solely for deserializing the request body ("filename", "data", "extension") which is then used to construct the desired entity class ("filename", "link", "extension") that is to be stored,
c) define a custom JsonDeserializer that parses the request body and constructs the entity class ("filename", "link", "extension").
While all of the approaches will probably work, in a more complex scenario, option a) will clutter the entity's class with members that are only required for deserializing once, while option b) and c) may result in a lot of additional classes.
Is there any best practice for this? Btw, thanks for reading this far :)

Wrapping a Web API response in JSON but still having it work with IQueryable and oData

I have an ASP.NET Web API project, and I want to use oData filters in the project, with the ASP.NET oData preview package. This means I need to use IQueryable as the response type.
Unfortunately, the consumer requires the response wrapped like so:
{
"total": 2,
"success": true,
"data": [
{ object1 },
{ object2 } ]
}
I created a wrapper object which assigns the IQueryable response from my original version to the "data" property, and sets the values for the "total" and "success" properties as well. This creates the JSON response the consumer is looking for. But it's no longer IQueryable, which means I can't implement oData filters.
I was hoping to take my wrapper object and make it implement IQueryable by setting its enumerators, etc. However, I don't think this will work because I have to assign the Provider and Expression properties to implement the interface, and I don't know what I should put there. I'm using a Repository pattern with IoC, and the EntityFramework Code First data access is in a separate project, with concrete objects assigned to the interface placeholders using Ninject. Perhaps I can abstract out the interfaces all the way from the data access layer so that the the Repository objects carry a reference to an IQueryProvider. Do you think this would work, and allow it to support the automated oData integration?
Here's how you do it. Behind the scenes, [Queryable] just creates an instance of ODataQueryOptions and then applies it to the queryable you return. The good news is that you can parameter bind ODataQueryOptions and have code that looks like this:
public Wrapper<MyObject> Get(ODataQueryOptions<MyObject> queryOptions)
{
IQueryable<MyObject> queryResults = queryOptions.ApplyTo(dbSet);
return Wrap(queryResults);
}
You don't even need [Queryable] anymore because you're doing what it would have done for you. Hope that helps.

Any mock tool which will return a preset JSON string?

I would be wanting a tool which can mock a RESTFul server and it should return the preset JSON data which I have mapped for a particular URL.
For ex: If I call http://ccccc.com/api/users , the mockup tool should return users ( which I have already preset ) in JSON format.
This might be useful when I create client side code with backbone or jquery to get back the models using an ajax call.
Any suggestions for this kind of mockup tool ?
Note: Creating a servlet which reads the incoming GET url and reads the preset JSON from a file and outputting it as JSON string is possible. But I am looking for a tool which will do this for me.
Sinon.js is what you're looking for. In particular the sinon.fakeServer API.
https://github.com/homerquan/kakuen
Mock up RESTful webservices simply by editing text files, e.g.,
GET_#book#123#authors.json ==> GET /book/123/authors
POST_#book#id=123.json ==> POST /book?id=123
For json, a schema-based mockup is supported, e.g., in sample_server/mocks/GET__#search#q=js.json
e.g.,
"#KAKUEN_ITEM(offset)": {
"#KAKUEN_TYPE": "natural",
"#KAKUEN_PARAM": {
"min": 1,
"max": 20
}
}
will be offset:12

jax-rs pass more than one JSON object to java method call

I am using CXF Jax-rs, wanted to pass 2 JSON objects as arguments to my rest service method.
I was able to pass one object as argument.
Appreciate any help.
thanks,
Bob
I assume by passing a "JSON object" (not the best terminology but I think I know what is meant here) you mean you are passing a parameter to a service method via an entity body, and specifying its content type.
As you know in JAX-RS only one parameter can be bare (that is, not marked with #PathParam or #QueryParam or #MatrixParam etc.) and that single unannotated parameter comes from the entity body.
So you cannot pass two such parameters. It is not allowed.
What you can do is encode your entity body of the request in JSON as follows:
{ "param1": { .... }, "param2": { .... } }
which fakes two "objects" into one.