I've just integrated RestKit with a Mac application for communicating with a web service. After much confusion, I have successfully got requests and responses working using it.
The problem I am now finding is that when I want to make a POST request.
I have created a RKRequestDescriptor with a mapping for a whole number of properties and all of the properties are being sent as parameters for the query. I want a way of dynamically changing the parameters that are sent, for example not sending some parameters where the property is nil.
Is this possible as part of the built-in functionality of RestKit? And if so, how?
You would need to use RestKit's Dynamic mapping class to handle mapping at run time.
Dynamic Object Mapping
RestKit supports such use cases via the RKDynamicMapping class.
RKDynamicMapping is a sibling class to RKObjectMapping and can be
added to RKRequestDescriptor and RKResponseDescriptor objects and used
to configure RKMappingOperation instances. RKDynamicMapping allows you
to hook into the mapping process and determine an appropriate concrete
RKObjectMapping to use on a per-object basis.
Or you could not use RestKit and set the POST body yourself. Create the required dictionary by adding only required parameters. Serialize this object with help of NSJSONSerialization and set this NSData object as HTTP Body in the request instance.
Related
I have a sling servlet that invokes a 3rd party api and fetches a json response. I have mapped the json response to a pojo class using Jackson. I now have to display this dynamically fetched and mapped response in sightly. How do i do that? I am stuck after the response mapping
With the new version of Sling Models, you can directly expose a model as a Servlet by specifying a resource type and the selector to use in your model annotations. When the Model is loaded into Apache Sling, it automatically registers a Servlet corresponding to the model, allowing you to with nearly zero additional code, create a Servlet to access a JSON representation of the model. That’s super cool!
The above life makes your Life Easier!!
You can have all your objects in Sling Model. Since the sling model acts as a servlet You can make the AJAX call and get a real-time response.
Please refer to this document.
https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
The correct path is:
HTL/Sightly -> Sling Model -> OSGi Service -> External API
So you have to extract the code that fetches the data into an OSGi service.
But please secure your code that calls the external API. As example if the External API is not responding or is extremely slow, it could consume all available threads of AEM. Then AEM could be completely unusable. To secure it, you could use as Example a Semaphore.
Assuming the JSON returned is arbitrary, the best thing to do is simply display it as a string. To do that, instead of mapping the JSON response to a POJO I would recommend adapting a Sling model to the response.
Then, you can set that Sling model to be the model in your sightly code, using data-sly-use.model, and in the Sling model constructor you can set the response value to an attribute of the sling model.
Then all you'd need to do is put that attribute in a ${} in the sightly html.
If the format/structure of the JSON isn't completely unknown, you could use the POJO in the sightly. Create some conditionals to test what attributes the POJO has, so you can put them into the sightly code.
In our project we expose a number of web-services that were generated from a wsdl. After generating them, I can see that the requests and responses are mapped to POJOs and when I am making the response, I just set a new POJO. This works really nice. However, I have a problem with the request. When we receive the request I expected that the payload will be a POJO mapping the parameters from the request. The payload becomes actually an array of objects. I can access the values but this is not very comfortable. You can take a look at the picture.
I can see that the under "Variables" in the method it is correctly matched to the POJO we would like to have. Is there some setting that I am missing somewhere so that we can get the payload to be mapped to correct POJO type?
Re-run the WSDL to Java codegen but this time with wrapper style disabled, see: https://cxf.apache.org/docs/wsdl-to-java.html#WSDLtoJava-wrapperstyle
I'm trying to send a POST request with RestKit. I have an entity (called workout) that I want to serialize and send to my server backend.
The workout Entity has-one related Entity (called exercise) which has a name.
The Problem I have is that my server expects the JSON in a little bit different format than what RestKit sends:
What the backend receives is the following: exercise[name]. Note that the name attribute of the related entity exercise is in square brackets.
My Server expects these attributes in the following form: exercise.name.
My question is: How can I tell RestKit to use a dot instead of square brackets when serializing entities?
I've looked into setSourceToDestinationKeyTransformationBlock but I could'nt figure out how to use it to solve my problem.
By default RestKit will use form URL encoded serialisation (and that's what it looks like you're getting). To correct this, set the serialisation type to JSON:
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
When creating a web application, if we use third party api calls , which themselves provide response objects, is it a good idea to directly use those response objects in the view /model? Or do we need to create a model object that copies the data from these reponse objects, just so that they are decoupled from the third party objects? Is this decoupling required always even when there is no other data attribute to add , other than the already existing data in the response object? Also these response objects seem to have no setter methods but only read methods? Does this information alter the design decision is any way?
What is the good practice here?
I would not have dependency on external response objects while building the model objects. If the model object needs attributes from the response, I would pass the attributes instead. This helps in mocking the model objects during testing without much hassle.
When it comes to the view objects, I would think it to be fine to depend on the response objects to extract attributes from them during rendering.
My two cents!
In my app, I have this scenario where I need to post an object to remoter server and get an object key back and then store the object locally. I have Core data and Restkit implemented in my app.
The object value are collected from user input. I couldn't figure out a great way to prepare the object before posting it to remote server. This object is an entity of type NSManagedObject, and I don't want to store it before I get the object id from server.
I came across this which suggested to use a transient object to handle this situation. But as discussed in that thread, this causes issue with code maintenance.
Is there a better way to handle this scenario? Thanks.
Make your core data model class adhere to the RKRequestSerializable protocol.
Then when the user input is validated, create an entity as normal and set it as the params value to the RKRequest, this will send your object as the HTTP body. Look inside RKParams.m for an example.
Also set the newly created entity as the targetObject for the RKObjectLoader. That way, when your web service returns the information (like the new unique ID), it will target the new object and save the new unique ID to this object without creating a duplicate.
Clear as mud?
PS: Oh and be careful mixing autogenerated core data classes with custom code! I recommend mogen to help you not lose code each time you make a change.