30、关于kie-server代码请求方式JSON,XML

Kie-server 是什么,请参考: tomcat下安装kie-server
请求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());


}

下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的

----请看下方↓↓↓↓↓↓↓

百度搜索 Drools从入门到精通:可下载开源全套Drools教程

深度Drools教程不段更新中:


更多Drools实战陆续发布中………

扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值