Help creating document in API

Hi. I’m trying to make a custom API for creating documents. I’m following the example, but getting ClassCastException with the wpm.createObject() method.

WorkflowPersistenceManager wpm = (WorkflowPersistenceManager) getPersistenceManager(requestContext);

//HippoFolderBean contentBaseFolder = getMountContentBaseBean(requestContext);  //returns null
//String eventDocumentFolderPath = contentBaseFolder.getPath() + "/eventDocument";  //throws NPE
String beanPath = wpm.createAndReturn(
                        path,  //where the content will go -  comes from payload
                        "cm:"+documentTypeName,  //where the document type definition is - workaround because of nulls above
                HippoDocument hippoDocument = (HippoDocument) wpm.getObject(beanPath); //works
//                EventDocument eventDocumentBean = (EventDocument) wpm.getObject(beanPath);  //throws ClassCastException

java.lang.ClassCastException: class org.hippoecm.hst.content.beans.standard.HippoDocument$EventDocument$4vkQoxwy cannot be cast to class (org.hippoecm.hst.content.beans.standard.HippoDocument$EventDocument$4vkQoxwy and are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @3bb44eac) 

Any ideas?
Thank you.

Hi @Joshua_Glickman
What is the specification of EventDocument class, is that a subtype of HippoDocument?

Hey @Joshua_Glickman ,

are you creating this custom API in the CMS or Site context?
Have you generated the bean java class for the EventDocument with Essentials?

Because the cast exception you see is probably due to the Dynamically generated beans (notice EventDocument$4vkQoxwy and EventDocument).
Read about this here and notice at the end

If your project contains custom REST resources, it is recommended that you mark the related content bean classes as not modifiable.

Kind regards

To create a document using an API, you will need to follow these general steps:

  1. Choose an API: Select an API that supports document creation. Some popular options include Google Drive API, Microsoft Office 365 API, and Dropbox API.
  2. Set up authentication: Authenticate your application with the API by obtaining an access token or API key.
  3. Create the document: Use the API to create a new document. This may involve specifying the document type, format, and location.
  4. Add content: Use the API to add content to the document. This may involve uploading files, inserting text or images, or applying formatting.
  5. Save the document: Use the API to save the document to the desired location.
  6. Test and refine: Test your document creation process and refine it as needed to ensure that it works as expected.

Keep in mind that the specific steps involved in creating a document using an API may vary depending on the API you choose and the requirements of your application. Be sure to refer to the API documentation and follow any guidelines or best practices recommended by the API provider.
Click here more information: