30、关于kie-server代码请求方式JSON,XML
2016-09-09 14:34
274 查看
Kie-server 是什么,请参考:tomcat下安装kie-server
请求kie-server 常用的有两种请求方式,一种是JSON,另一种是XML方式。在项目中,这个是经常会用到的,
下面我们通过代码,将这两种方式做一个简单的说明:
XML方式:
JSON方式:
请求kie-server 常用的有两种请求方式,一种是JSON,另一种是XML方式。在项目中,这个是经常会用到的,
下面我们通过代码,将这两种方式做一个简单的说明:
XML方式:
webservicecxf.webservicecxf.Person person=new webservicecxf.webservicecxf.Person(); person.setName("张小三"); String url = "http://192.168.80.200:8080/kie-server/services/rest/server"; String username = "kieserver"; String password = "kieserver1!"; InsertObjectCommand insertObjectCommand1 = new InsertObjectCommand( person, "InputTransaction" );//InputTransaction 输入事务,在请求获取请求时,和web请求获取一样 GetObjectsCommand getObjectsCommand = new GetObjectsCommand();//将当前会话中集合返回 getObjectsCommand.setOutIdentifier( "command" ); //从标识符 command FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand( "RunAllRules" );//对规则的的处理 List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>(); commands.add( insertObjectCommand1 ); commands.add( fireAllRulesCommand ); commands.add( getObjectsCommand ); BatchExecutionCommandImpl command = new BatchExecutionCommandImpl(commands); command.setLookup("kiesession02");//指定kiesession的id String xStreamXml = BatchExecutionHelper.newXStreamMarshaller().toXML( command );//将请求内容设置成为XML KieServicesConfiguration config = KieServicesFactory.newRestConfiguration( url, username, password );//登录服务器 config.setMarshallingFormat( MarshallingFormat.XSTREAM );//请求方式 KieServicesClient client = KieServicesFactory.newKieServicesClient( config ); String containerId = "test3";//这里容器的名称 KieServerCommand call = new CallContainerCommand( containerId, xStreamXml ); List<KieServerCommand> cmds = Arrays.asList( call ); CommandScript script = new CommandScript( cmds ); ServiceResponsesList reply = client.executeScript( script ); for (ServiceResponse<? extends Object> rs : reply.getResponses()) { System.out.println( rs.getResult() ); if (rs.getResult() != null) { ExecutionResultImpl result = (ExecutionResultImpl) BatchExecutionHelper.newXStreamMarshaller().fromXML( (String) rs.getResult() ); person = (webservicecxf.webservicecxf.Person) result.getResults().get( "InputTransaction" ); //和web 获取前端传值很像吧 ArrayList<Object> objects = (ArrayList<Object>) result.getResults().get( "command" );//返回当前对象的所有集合 } else System.out.println( "Empty result...?" ); } }
JSON方式:
@Test public void test00json2(){ School s = new School(); s.setName("一班"); s.setCount(20); String url = "http://10.0.5.103:8080/kie-server/services/rest/server"; String username = "kieserver"; String password = "kieserver1!"; KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(url, username, password); config.setMarshallingFormat(MarshallingFormat.JSON); config.setTimeout(30000L); KieServicesClient client = KieServicesFactory.newKieServicesClient(config); RuleServicesClient rules = client.getServicesClient(RuleServicesClient.class); KieCommands cmdFactory = KieServices.Factory.get().getCommands(); List<Command<?>> commands = new LinkedList<Command<?>>(); commands.add(cmdFactory.newInsert(s, "school")); //输入事务,在请求获取请求时,和web请求获取一样 commands.add(cmdFactory.newFireAllRules()); ServiceResponse<org.kie.api.runtime.ExecutionResults> response = rules.executeCommandsWithResults("asdasd",cmdFactory.newBatchExecution(commands, "jars2kession")); //第一个参数,容器名称,第二个参数将传放的值放到容器中 jars2kession 表示kiesession System.out.println(response.getMsg()); ExecutionResults result = response.getResult(); //获取请求 ServiceResponse.ResponseType type = response.getType(); //请求状态 System.out.println(type.name()); s = (School) result.getValue("school"); //和web 获取前端传值很像吧 System.out.println(s.getName()); }
相关文章推荐
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- Android解析XML(PULL方式)和JSON 工作原理和实现过程(详细代码)
- 手工代码校验请求参数,XML配置方式数据校验(企业主流校验)
- jquery json xml关于解析两次json数据动态取值的处理方式
- Android解析XML(PULL方式)和JSON 工作原理和实现过程(详细代码)
- 关于向后台请求数据(get请求,无参数传递),返回html代码(实际需要返回的是json数据)的解决方案
- Android Volley框架的几种post提交请求方式(xml->xml json->json xml->json)
- Android第一行代码 第九章 数据解析方式(xml,json)
- Vue-cli 使用json server在本地模拟请求数据的示例代码
- 关于原生JavaScript的http全部请求 post get json xml file 全了 拿去救急
- 关于将两个LinearLayout添加到RelativeLayout里代码的添加方式和xml两种形式
- 关于快速查看JSON 或 XML 格式字符串的几种方式
- Android解析XML(PULL方式)和JSON 工作原理和实现过程(详细代码)
- webpack4.0.1_vue脚手架的项目与json-server结合,vue开发前端时用json-server模拟数据,fetch的请求代码
- 关于James Clark的XML, JSON和其它许多事情
- 关于James Clark的XML, JSON和其它许多事情
- 关于James Clark的XML, JSON和其它许多事情
- web-xml与server-xml配置不同形成应用不同的访问方式
- struts2.0关于请求jsp方式上传的问题
- java中操作xml几种方式的比较和代码示例(DOM,SAX,JDOM,DOM4J)