您的位置:首页 > Web前端 > JavaScript

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

2016-09-09 14:34 274 查看
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 应用 json xml
相关文章推荐