How to run java workflow with SpringMVC
Technologies used in this article :
- Neuro4j Workflows 2.0.1
- SpringMVC 4.1.0.RC1
- Neuro4j Studio 1.0 (based on Eclipse Kepler)
- JDK 1.6
1. Download project.
Download project and unzip it.
Download java project – SpringMVCExample.zip
2. Import project into Eclipse
3. Source code:
- Custom service interface - org.neuro4j.workflow.tutorial.mvc.service.UserService.java
1 2 3 4 5 6 7 package org.neuro4j.workflow.tutorial.mvc.service; public interface UserService{ public String serve(String fruit); }
- Custom implementation - org.neuro4j.workflow.tutorial.mvc.service.UserServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 package org.neuro4j.workflow.tutorial.mvc.service; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { public String serve(String fruit) { return "I like " + fruit + "!"; } }
- CustomBlock which calls service. Spring will set service implementation during initialization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package org.neuro4j.workflow.tutorial.mvc.views; import static org.neuro4j.workflow.tutorial.mvc.views.BlockWithService.IN_FRUIT; import static org.neuro4j.workflow.tutorial.mvc.views.BlockWithService.OUT_MESSAGE; import org.neuro4j.workflow.FlowContext; import org.neuro4j.workflow.common.FlowExecutionException; import org.neuro4j.workflow.common.FlowInitializationException; import org.neuro4j.workflow.common.ParameterDefinition; import org.neuro4j.workflow.common.ParameterDefinitionList; import org.neuro4j.workflow.node.CustomBlock; import org.neuro4j.workflow.tutorial.mvc.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @ParameterDefinitionList(input = { @ParameterDefinition(name = IN_FRUIT, isOptional = true, type = "java.lang.String") }, output = { @ParameterDefinition(name = OUT_MESSAGE, isOptional = true, type = "java.lang.String") }) @Component public class BlockWithService extends CustomBlock { static final String IN_FRUIT = "fruit"; static final String OUT_MESSAGE = "message"; @Autowired private UserService userService; public int execute(FlowContext ctx) throws FlowExecutionException { String fruit = (String) ctx.get(IN_FRUIT); // if (fruit == null) { return ERROR; } String message = userService.serve(fruit); ctx.put(OUT_MESSAGE, message); return NEXT; } @Override public void init() throws FlowInitializationException { super.init(); } }
- Flow
- Spring Controller which extends org.neuro4j.workflow.springmvc.AbstractWorkflowController;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package org.neuro4j.workflow.tutorial.mvc.views; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.neuro4j.workflow.FlowContext; import org.neuro4j.workflow.common.FlowExecutionException; import org.neuro4j.workflow.springmvc.AbstractWorkflowController; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class ViewsController extends AbstractWorkflowController{ @RequestMapping(value="/simpleview", method=RequestMethod.GET) public ModelAndView prepare(Model model, HttpServletRequest request, HttpServletResponse response) throws FlowExecutionException { model.addAttribute("fruit", "apple"); FlowContext context = processWorkflow("org.neuro4j.workflow.tutorial.mvc.views.ViewFlow-Start", model, request, response); return createModelView(context); } }
- servlet-context.xml defines <beans:bean class="org.neuro4j.workflow.springmvc.WorkflowWebApplicationListener" />.. Bean will set up initializer for CustomBlock.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <annotation-driven /> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <beans:bean class="org.neuro4j.workflow.springmvc.WorkflowWebApplicationListener" /> <task:annotation-driven /> <beans:import resource="controllers.xml" /> </beans:beans>
4. Build project.
mvn clean install
5. Start your tomcat and deploy war to tomcat_home/webapps folder.
Open http://localhost:8080/SpringMVCExample/ in your browser.
Hello from SpringMVC!
Click on link to call SpringMVC controller.
Download Source Code
Download java project – SpringMVCExample.zip