Open goldyliang opened 4 years ago
You can get an instance of DocumentMapper
(which behind the scenes makes use of ResourceMapper
from CrnkBoot/ModuleRegistry. This gives a Document
object that can easily be translated to JSON with Jackson.
Thanks @remmeier . So following your suggestion, my code would be something like below.
I verified that it works fine.
DocumentMapper mapper = crnkBoot.getDocumentMapper();
QuerySpec querySpec = new QuerySpec(MyEntity.class, "type-name");
QueryAdapter adapter =
new QuerySpecAdapter(
querySpec,
filterRequestContext.getQueryAdapter().getResourceRegistry(),
filterRequestContext.getQueryAdapter().getQueryContext());
Document result = mapper.toDocument( new JsonApiResponse() {{
setEntity(entity);
}}, adapter, crnkBoot.getModuleRegistry().getDocumentMappingConfig()).get();
return new Response(result, HttpStatus.CREATED.value());
But the next question is, what is the correct way to get CrnkBoot instance? I tried auto-wired it in the document filter, but the application does not start, seems like due to some initialization order issue.
Now I had to create a component to extend CrnkBootConfigurer so as to get the instance and remember it.
I have a need to convert an entity object to a Document object so it can be returned to user directly, but can not find out the obvious way.
The code I am targeting is like below:
The only way I can find (which is ugly), is like below: