How to store Response.ok().built result into a String variable

Dear experts,

Referring to my previous Topic “Return Object as POJO”.

With the help of the good and nice experts in this forum, I have managed to create my POJO and return it in a REST API call.

My previous code:

	@GET
	@Path("/test")
	public Response test(@Context HttpServletRequest request) throws Exception {
		Session session = null;
		Orang_POJO tmp= null;
		try {
			HippoRepository repository = HippoRepositoryFactory.getHippoRepository("vm://");
			session = repository.login("admin","admin".toCharArray());	
			Node node = session.getRootNode().getNode("content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang");	
			HstRequestContext context = RequestContextProvider.get();		
			Orang org = (Orang) context.getObjectConverter().getObject(node);
			tmp = new Orang_POJO(org);
		}catch(Exception e) {
			e.printStackTrace();
			throw  e;	
		}finally {
			session.logout();			
		}
		return Response.ok(tmp,MediaType.APPLICATION_JSON).build();
	}

The result is :
{“nama”:“Sola”,“age”:“48”}

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”}

Can it be done?

Regards
Sola Lee

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:

          try {
               HstRequestContext requestContext = RequestContextProvider.get();
               Orang org = (Orang) requestContext.getObjectBeanManager().getObject("/content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang");
               // ...
          } catch (Exception e) {
              log.error("your error log...", e);
          }    

I think so. Did you try it?

Regards,

Woonsan

1 Like

Dear @woonsanko ,

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.:rofl:

Regards
Sola Lee

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.

Dear @woonsanko,

Yes I agree with you.

Is it possible to get that converted JSON (done by HIPPO CMS) and place that result into a String?

Regards
Sola Lee

It is possible. You can follow the section 3.1 from this guide:

Regards,

Woonsan

1 Like

Dear @woonsanko,

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);

Gives an error :

 javax.jcr.PathNotFoundException: content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang

But it works only after I login.

	HippoRepository repository = HippoRepositoryFactory.getHippoRepository("vm://");
	session = repository.login("admin","admin".toCharArray());	
	Node node = session.getRootNode().getNode("content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang");	
	HstRequestContext context = RequestContextProvider.get();		
	Orang org = (Orang) context.getObjectConverter().getObject(node);
	tmp = new Orang_POJO(org);

Thank you and appreciate the effort.

Regards
Sola Lee

Dear @woonsanko,

Thank for sharing the links to * https://www.baeldung.com/jackson-object-mapper-tutorial , it works.

Now I can format my JSON properly.

Regards
Sola Lee

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);
        // ...

Woonsan

[1] http://javadoc.onehippo.org/12.0/hippo-site-toolkit/
[2] https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/index.html

1 Like