Sending an Object as a Parameter to GET Request: PlayFramework - rest

I have a class Caller:
class Caller{
String caller;
String callTo;
String x, y , z;
}
Now, I want to send a GET request:
localhost:9000/api/v1/call
But I want to send and Object "obj" of Type Caller as the parameters to this Request, so that my receiving api will be simply able to get everything it needs from the Caller object.
I saw a lot of material in the official PLay Documentation; all talking about Templates. I dont think I am looking in the right direction.
Can someone please help me proceed in the right direction?
Any example on how to send my "Caller" object as Parameter to GET REQUEST?
PS: I am using Play Framework 2.3.X in Java.

Related

Pass ID or Object which has irrelavant details as RequestBody in Rest Call?

Approach 1:
#PostMapping("/api/{id}")
String getSomeObj(int id){
//make another rest call with id and get CustomObj
// then do some logic and return something
//Here response time will be more as it has again another rest calls
}
Approach 2:
#PostMapping("/api/{id}")
String getSomeObj(#PathParam("id") int id, #RequestBody CustomObj obj){
//directly do logic with the provided obj and return something
//Here Response time would be less as we are directly getting the actual Object from Request Body
//BUT is this a good practise to pass an object in which we need only few details?
}
Q1) All I am asking is to whether to pass just id or Object? If id is passed, another Rest call has to be made unnecessarily. If Object is passed, we can avoid making another rest call, BUT the problem is: this custom object may contain some irrelavant details too.. So, is this correct?
Q2) If passed with id, response time will be more when comparing with just passing object.. So, I am not understanding which approach should follow..
A1) This is all up to you and there is no "one correct" way. I would say if it's a small object pass the object and respond fast. If its a big object pass the id. How do you define big and small objects? if object has hashmaps or lists in it that's a big object. Also you can ignore serialization of internals; check https://www.baeldung.com/jackson-ignore-properties-on-serialization
A2) Pass the id and enjoy your REST service. After all REST is very fast. Don't worry about the speed of calls. If your back end function is fast and if you put a "loading" gif to front end; users will wait for the response.

REST API to check if an object exists

I have currently a webservice that load an object which looks like /object/load?id=100, the problem is that my object is really huge and it takes a long time to get the full response of the webservice just to see if the object exists or not.
What is the best pratice here ?
Creating a new webservice /object/exists?id=100 thats only use HTTP status code (200 if object exists, 404 if not) ?
Add parameter to the /object/load webservice to return only simplified object ?
If you are only interested in existence, or knowing in advance how the API will respond if you do a real GET request, HTTP actually has a built-in method for that: HEAD.
I'd recommend creating a new method within your existing web service. Name that method 'Exists' or something similar, and make sure that it is a HttpGET method.
You could then pass in the ID of the object you want to check, then within the method just do a check to see if the object exists. If the object exists you should return true, else return false.

Swagger UI generation for generic REST calls

What do I want:
I want to be able to generate swagger documentation that passes a key/value into the URL. This so that I can use generic arguments controller to handle my requests like Dictionary.
If swagger can't generate it, is there a way to generate the documentation by using reflection on my objects? This so that I can still use generic methods
If not, what would be the best way to let everyone know what the correct approach would be.
Why do I want it
I'm developing a new API and I'm using swagger to create the documentation. In this API I want to work with some generic methods to prevent hardcoding things. For example on the PATCH method I use a Dictionary<string, string> to get the property/value combination and in the GET I use a custom object as the argument. In both cases, swagger can't generate the correct parameter fields, because it takes the argument as url key.
Example action & form - incorrect
public async Task<IActionResult> Patch(int id, Dictionary<string, string> viewModel)
{
return await ConnectionWrapper(() => connector.Patch(id, viewModel));
}
This uses the body, not the query
Other examples - incorrect
In the GET I have a model with a custom modelbinder to handle all the rest URL arguments. The problem is because the model is defined it sees the filter as a property.
Then it is in the URL, but it will look like http://example.com/controller/method/id?sort=prop_asc&filter=propTwo%3D=value, instead of http://example.com/controller/method/id?sort=prop_asc&propTwo=value
Desired output
I've modified the HTML to simulate what I would like in the picture above. The URL that would be called would be http://example.com/controller/method/id?propertyName=propertyValue.
I don't mind if there would be only one option to add a generic key/value pair because with it I can demonstrate what I want.
Expected solution
I think the solution lies in the MapType startup method of swagger or in an implementation of the IOperationFilter, but I haven't been able to figure it out.

What is an entity in Akka-Http?

I am new to akka-http and building a basic server-client application in scala. The examples I looked at has the object "entity". Can someone please explain the concept underlying and why is it used and how is it useful?
post {
path("insert") {
entity(as[Student]) {
obj => complete {
insertingstudent(obj)
s"got obj with name ${obj.getName()}"
}
}
Thanks
Can someone please explain the concept underlying and why is it used
and how is it useful?
entity is of type HttpEntity. From the comments of the code:
Models the entity (aka "body" or "content") of an HTTP message.
It is an abstraction over the content of the HTTP request. Many times, when one sends an HTTP request, they provide a payload inside the body of the request. This body can be in many format, popular ones are JSON and XML.
When you write:
entity(as[Student])
You are attempting to unmarhsall, or deserialize, the body of the request into a data structure of your liking. That means that your obj field in the proceeding function will be of type Student.

Post array of object to WCF Rest service using JSON?

Need to send a complex object having nested IList from Iphone in JSON format to WCF REST service. The Complex Object is defined as following:
public class BatchData
{
long BatchID;
List<Account> Accounts;
List<Contacts> Contacts;
}
Please let me know the Client side (IPhone) syntax to create the required JSON request and also the server side POST method implementation to handle such scenario ?
Thanks in advance.
For the server side: define an operation contract which takes an array of that type. Search for examples on WCF Rest services (you'll need to use the [WebInvoke] attribute to define your operation) and you'll find how to do it.
For the client side: you can send the request using the NSURLRequest class. And to create the appropriate JSON, you can look at the NSJSONSerialization class, which will help you to convert between arrays (NSArray) and dictionaries (NSDictionary) and the JSON you need to send to the service.