cancel
Showing results for 
Search instead for 
Did you mean: 

How to start a process via Rest API using java code?

magda
Champ in-the-making
Champ in-the-making
Hi,

I am new in Activiti.  I use Activiti 5.17.0. I have a problem with starting process using java code.
I have created an activiti diagram (process.bpmn20.xml) with start forms created in Activiti plugin eclipse.
I attach my bpmn diagram with forms.

My process definition is visible in Activiti Explorer->Processes->Deployed process definitions …
I can start instance of process using REST but i must use java code in eclipse.

I have to start process with values in forms.
Optionally can be another solution without Rest Api.

I tried to do it like this:   http://docs.alfresco.com/activiti/topics/api.services.start.processinstance.html but unfortunately my process didn't startSmiley Sad

Please forgive me my poor knowledge about Activiti… I'm newbie.


Please help,
Magda
2 REPLIES 2

magda
Champ in-the-making
Champ in-the-making
Also i tried start proces with this code but nothing happens:


package activiti;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.TreeMap;

import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class MyUnitTest
{
   private static String HOST = "localhost";
  private static String PORT = "8080";
  private static String PROTOCOL = "http";
  public static String BASE_URL = PROTOCOL+"://"+HOST+":"+PORT+"/activiti-rest/";
 
                  TreeMap<String, Object> mainParams= new TreeMap<String, Object>();
                  mainParams.put("processDefinitionKey", "vacReq");
                 
    RestTemplate restTemplate = new RestTemplate(getAuthenticatedHeader());
    URI uri = restTemplate.postForLocation(BASE_URL+"service/runtime/process-instances", mainParams ,mainParams);
 
private SimpleClientHttpRequestFactory getAuthenticatedHeader() {
 

   SimpleClientHttpRequestFactory connectionFactory = new SimpleClientHttpRequestFactory() {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
     super.prepareConnection(connection, httpMethod);
     HttpHeaders headers = new HttpHeaders();
     headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
 
     String authorisation =   "kermit:kermit";
     byte[] encodedAuthorisation = Base64.encodeBase64(authorisation.getBytes());
     connection.setRequestProperty("Authorization", "Basic "+new String(encodedAuthorisation));
     connection.setRequestProperty("Content-Type", "application/json");
     connection.setRequestProperty("Accept", "application/json");
    }
   };
   return connectionFactory;
  }
}

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Magda,

following jUnit test from activiti source could help you:
org.activiti.rest.service.api.runtime.ProcessInstanceCollectionResourceTest#testStartProcess

Regards
Martin