Conversion from POJO to JSON is done in the Response where the code built with the MediaType.APPLICATION.JSON.
How can I use the Response object “Response.ok(tmp,MediaType.APPLICATION_JSON).build();” to store the result in a String. Something like the following:
String data = Response.ok(tmp,MediaType.APPLICATION_JSON).build();
data = "items : " + data;
Response.ok(data ,MediaType.APPLICATION_JSON).build();
The result I wish to get is :
items: {“nama”:“Sola”,“age”:“48”}
As you are able to get HstRequestContext here, meaning your REST code resides in HST app (e.g, /site), you don’t need to access HippoRepository and HippoRepositoryFactory.
Instead, you can:
Thank you, appreciated the advise on repository and session.
Yes I have tried to:
String data = Response.ok(tmp,MediaType.APPLICATION_JSON).build().toString();
But it did not work because build method returns a response and not sure which method to use to extract the processed content. Obviously I tried toString() and obviously I got the Response Object class with the hashcode.
You’re not supposed to transform the tmp object to String by yourself. Just return Response object as your method was supposed to return Response object.
JAX-RS runtime framework, Apache CXF in brXM (aka Hippo CMS), will convert the Response object to a JSON response automatically. Just try to do return Response.ok(tmp,MediaType.APPLICATION_JSON).build() at the end of the method and check if the response is okay from an http client.
Thanks for the tips on `HstRequestContext.
From my code i found out running HstRequestContext without HippoRepository and HippoRepositoryFactory did not work.
HstRequestContext context = RequestContextProvider.get();
session = context.getSession();
Node node = session.getRootNode().getNode("content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang");
Orang org = (Orang) context.getObjectConverter().getObject(node);
tmp = new Orang_POJO(org);
First, I’d recommend you to always try to retrieve a document node first instead of trying to retrieve a compound node, any descendant node of a document variant node.
Second, by default, you can get a node through HstRequestContext#getSession() only when the specific document at the path was published.
So, could you make sure you published the document first and check if you can find the document through document handle path (because HST resolves a proper document variant automatically), and finally get or iterate child compound nodes using HST API [1] and JCR API [2]?
Example:
HstRequestContext requestContext = RequestContextProvider.get();
HippoDocumentBean document = (HippoDocumentBean) requestContext.getObjectBeanManager().getObject("/content/documents/xxxxxx/cubaorang");
// You can get Node through document.getNode() here, but stick with HST API wherever possible like the following.
Orang org = document.getBean("xxxxxx:Orang", Orang.class);
// ...