How to post data through Java backend using Ajax from embedded user task form from front end

I am very new to Camunda. I have a successfully running Camunda application. I have a user task with start/end execution Java listener classes. But, I have a requirement where I have to post some data.
I am trying to send data with Ajax POST method. On same Camunda application I am creating a servlet Java class with overridden doPost method. Servlet class name is PostFormNotesServlet

@WebServlet(“/PostFormNotesServlet”)
public class PostFormNotesServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(PostFormNotesServlet.class.getName());

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
    Map<String, String[]> dataInfo = request.getParameterMap();
    log.info("+++++++++++++++++++++++++++++++++++++++++++++++");
    log.info(dataInfo.get("transactionId").toString());
    log.info(dataInfo.get("notes").toString());
    log.info("+++++++++++++++++++++++++++++++++++++++++++++++");
    
}

}

and on user_task.html I am doing following:–

$http({
url: ‘PostFormNotesServlet’,
method: ‘POST’,
data: formData,
transformRequest: angular.identity,
headers: {‘Content-Type’: undefined}
});

For url localhost:8080/engine-rest/process-definition/key/MyProcessId/PostFormNotesServlet, I am getting following error:–

Could not find resource for relative : /process-definition/key/MyProcessId/PostFormNotesServlet of full path: http://localhost:8080/engine-rest/process-definition/key/MyProcessId/PostFormNotesServlet

Where am I doing wrong? Please help.

Since you mentioned Ajax, I’d recommend a ReST interface via JAX-RS.

Roughly… your URL should look something like:

http://[myhost]:8080/[eclipse project name]/[service path]/[operation path]

Also, soapUI now supports ReST. They’ve improved support so you can now create a “rest project”.

Also, noticed you using “localhost”. Review your standalone.xml file and make sure it’s setup to receive intended requests. Basically, look for localhost IP and review port assignments. I typically setup the standalone.xml file to work with the actual hostname - this helps avoid (for me at least) confusion. It seems that I always bump into this issue when setting up a new JBoss/WildFly server. It’s easy to forget and only set once and then forgotten.

1 Like

@garysamuelson I really appreciate you for answering my question. I used jersey and it’s working. Thanks again.

Thanks & Regards
Alok Anand

Happy to help.

I noticed your class is serializeable:

This may not apply - but, Rest services tend to be @RequestScoped (though not required). Wasn’t sure how you were planning to serialize and manage the ReST implementation beans.

I tend to introduce services as “request-scope” and then later tune the implementation per performance requirements. But, that’s simply my preference…

1 Like

@Alok_Anand_Sharma - Thought you might find this interesting regarding custom ReST integration with Camunda’s BPM Engine.

I’ve switched over to directly using Jackson’s ObjectNode for input/output of these JAX-RS services. It makes for a very flexible implementation - avoiding having to update typed classes.

see:
GET/POST examples

https://github.com/garysamuelson/blogsource

see: Bpm.java and BpmSecure.java
Includes some in-line security examples - specifically showing security annotations

NOTE: I haven’t completed the write-up, or blog article… also a work-in-progress.

1 Like