First, Springboot integrated Activiti Modeler (Pit has stepped)

Source: Internet
Author: User
Tags bind json zip
first, integration preparation

Download Activiti source code and Activiti-5.22.zip

SOURCE download github address, after download use command git checkout 5.22.0-release switch to 5.22.0-release branch network address

Activiti-5.22.0.zip Download the official website, click No thanks,take me to download then enter the download interface. Network disk download Two, copy the project file into your project into the Modules\activiti-webapp-explorer2\src\main\webapp directory in the source file, copy diagram-viewer, Editor-app, modeler.html three files to the resources directory in the Springboot project. (mainly front-end page) decompression Activiti-5.22.0.zip, in the Activiti-5.22.0 Libs found Activiti-modeler-5.22.0-sources.jar, unzip it, will find the following three classes: ( Three controllers)
Copy the following three classes into the package for your Springboot project, as long as the package is in the lower-level package on the startup class, so that springboot will automatically scan to these three classes. Copy the source path Modules\activiti-webapp-explorer2\src\main\resources\stencilset.json to the resources directory in the Springboot project. (This file is the language of the control interface, attached to a Chinese file download address stencilest.json-Chinese to this point, all the required files are copied to complete, next, we began to configure the changes.) The project structure is as follows:
Third, modify the configuration

1. Add dependencies and refresh the project (this is Gradle, maven self-conversion)

    dependencies {
        compile (' org.activiti:activiti-spring-boot-starter-basic:5.22.0 ')
        compile (" Org.springframework.boot:spring-boot-starter-web ")
        testcompile (' Org.springframework.boot: Spring-boot-starter-test ')
        compile (' org.activiti:activiti-modeler:5.22.0 ')
        compile (' Commons-io: commons-io:2.5 ')
    }

2. Modify the startup class of the Springboot and add the following annotations on the class to turn off the security feature. The main reason is that Activiti's dependencies contain spring security's jar packages, so springboot automatically configures security features and requires a password when accessed.

@EnableAutoConfiguration (exclude = {
        Org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
        Org.activiti.spring.boot.SecurityAutoConfiguration.class,
})

3. Modify the App-cfg.js under the Resources directory, change the following (this is the path of the project, Springboot does not need the project path, so the project path is deleted)

Activiti. CONFIG = {
    ' contextroot ': '/service ',
};

4. Remember the three classes we copied in front of you.

Add requestmapping (value = "/service") annotation to the class name of each class. (This path is accessed by the page)

5. Modify the Modelsaverestresource class
(If you do not modify this, you will get an error in the page save process)

@RestController @RequestMapping (value = "/service") public class Modelsaverestresource implements

  modeldatajsonconstants {protected static final Logger Logger = Loggerfactory.getlogger (Modelsaverestresource.class);

  @Autowired private Repositoryservice Repositoryservice;

  @Autowired private Objectmapper Objectmapper; @RequestMapping (value= "/model/{modelid}/save", method = Requestmethod.put) @ResponseStatus (value = Httpstatus.ok) pub LIC void Savemodel (@PathVariable string modelid, @RequestParam ("name") string name, @RequestParam ( "Json_xml") string json_xml, @RequestParam ("Svg_xml") string svg_xml, @Re Questparam ("description") String description) {//modified the receive parameter try {model = Repositoryservice.getmodel (mode

      LID);

      Objectnode Modeljson = (objectnode) objectmapper.readtree (Model.getmetainfo ());
      Modeljson.put (Model_name, NAME); Modeljson.put (Model_description, Description);
      Model.setmetainfo (Modeljson.tostring ());

      Model.setname (name);

      Repositoryservice.savemodel (model);

      Repositoryservice.addmodeleditorsource (Model.getid (), Json_xml.getbytes ("Utf-8"));
      InputStream Svgstream = new Bytearrayinputstream (svg_xml.getbytes ("Utf-8"));

      Transcoderinput input = new Transcoderinput (svgstream);
      Pngtranscoder transcoder = new Pngtranscoder ();
      Setup output Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();

      Transcoderoutput output = new Transcoderoutput (OutStream);
      Do the transformation Transcoder.transcode (input, output);
      Final byte[] result = Outstream.tobytearray ();
      Repositoryservice.addmodeleditorsourceextra (Model.getid (), result);

    Outstream.close ();
      } catch (Exception e) {logger.error ("Error saving model", e);
    throw new Activitiexception ("Error saving model", e); }
  }
}
Iv. Preparation of tests

Create a new controller as follows

Import Com.fasterxml.jackson.databind.ObjectMapper;
Import Com.fasterxml.jackson.databind.node.ObjectNode;
Import org.activiti.editor.constants.ModelDataJsonConstants;
Import Org.activiti.engine.ProcessEngine;
Import Org.activiti.engine.ProcessEngines;
Import Org.activiti.engine.RepositoryService;
Import Org.activiti.engine.repository.Model;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
 /** * @author kingboy--KingBoyWorld@163.com * @date 2017/8/22 afternoon at 6:00pm * @desc. */@RestController @RequestMapping ("/activiti") public class Activiticontroller {/** * * Create model */@Request
            Mapping ("/create") public void Create (HttpServletRequest request, httpservletresponse response) {try {

            Processengine processengine = Processengines.getdefaultprocessengine (); Repositoryservice Repositoryservice = Processengine.getrepositoryservice ();
            Objectmapper objectmapper = new Objectmapper ();
            Objectnode Editornode = Objectmapper.createobjectnode ();
            Editornode.put ("id", "canvas");
            Editornode.put ("ResourceId", "canvas");
            Objectnode Stencilsetnode = Objectmapper.createobjectnode ();
            Stencilsetnode.put ("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
            Editornode.put ("Stencilset", Stencilsetnode);

            Model modeldata = Repositoryservice.newmodel ();
            Objectnode Modelobjectnode = Objectmapper.createobjectnode ();
            Modelobjectnode.put (Modeldatajsonconstants.model_name, "hello1111");
            Modelobjectnode.put (modeldatajsonconstants.model_revision, 1);
            String Description = "hello1111";
            Modelobjectnode.put (Modeldatajsonconstants.model_description, DESCRIPTION);
            Modeldata.setmetainfo (Modelobjectnode.tostring ());Modeldata.setname ("hello1111");

            Modeldata.setkey ("12313123");
            Save Model Repositoryservice.savemodel (Modeldata);
            Repositoryservice.addmodeleditorsource (Modeldata.getid (), editornode.tostring (). GetBytes ("Utf-8"));
        Response.sendredirect (Request.getcontextpath () + "/modeler.html?modelid=" + Modeldata.getid ());
        } catch (Exception e) {System.out.println ("Create Model failed:");
 }
    }
}

Start Project Access Localhost:8080/activiti/create, and see if you can see the edit page. The
configuration process must be careful, a little careless will fail, there are many pits, slowly solve the good

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.