Creating XML with JAXB KieServicesClient fails (KI

2019-08-28 10:31发布

问题:

I have an Object like this:

@XmlRootElement(name="com.Operation")
@XmlAccessorType(XmlAccessType.FIELD)
public class Operation implements java.io.Serializable
{

   static final long serialVersionUID = 1L;

   @org.kie.api.definition.type.Label("systemCode")
   @XmlElement
   private int systemCode;
   [...]

When I marshall this manually with following code it seems it works well:

[...]
JAXBContext jaxbContext = 
DroolsJaxbHelperProviderImpl.createDroolsJaxbContext(classNames, null);
Marshaller marshaller = jaxbContext.createMarshaller(); 
StringWriter xml = new StringWriter(); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(object, System.out);

Hence the console prints:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <com.Operation>
    <systemCode>1</systemCode>
    [...]

But when I use the KieServerClient encouraged code I get an error:

org.kie.server.client.KieServicesException: Error while serializing request data!
at org.kie.server.client.impl.AbstractKieServicesClientImpl.serialize(AbstractKieServicesClientImpl.java:514)
at org.kie.server.client.impl.AbstractKieServicesClientImpl.makeHttpPostRequestAndCreateServiceResponse(AbstractKieServicesClientImpl.java:234)
at org.kie.server.client.impl.RuleServicesClientImpl.executeCommandsWithResults(RuleServicesClientImpl.java:72)
[...]
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class com.Operation ni ninguna de sus superclases se conocen en este contexto.

(The is in Spanish but I guess the error is pretty easy translated)

A snippet of the code I use for JAXB with KieServerClient:

KieCommands commandsFactory = KieServices.Factory.get().getCommands();

List<Command<?>> cmds = new ArrayList<Command<?>>();
BatchExecutionCommand command = commandsFactory.newBatchExecution(cmds, SESSION_STATEFUL);

Command<?> insertObjectCommand = null;
insertObjectCommand = commandsFactory.newInsert(operation, operation.getClass().getName(), false, null); 
cmds.add(insertObjectCommand);
[...]
KieServicesConfiguration config =  KieServicesFactory.newRestConfiguration(KieServer.urlKieServer,
                        KieServer.name,
                        KieServer.password);
config.setMarshallingFormat(MarshallingFormat.JAXB); 
KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
RuleServicesClient rulesClient = client.getServicesClient(RuleServicesClient.class);  

ServiceResponse<ExecutionResults> response = rulesClient.executeCommandsWithResults("instances/"+containerName, command); 

Any ideas of what I am doing wrong and why one marshalling works but the other does not?

Except for the POJO, I currently do not have this project under maven since I want to know what jars I actually need to have these projects working and with maven a lot of dependencies are automatically resolved and I basically have no clue what I need (my .m2 is full with downloaded jars I have no idea of). Once resolved I will convert the project to maven.

Involved libraries are:

  • droolsjbpm-integration-distribution-6.5.0.Final
  • droolsjbpm-tools-distribution-6.5.0.Final
  • httpclient-4.5.3
  • httpcore-4.4.6
  • javax.ws.rs-api-2.0
  • jaxb-xjc-2.3.0
  • jms-1.1
  • kie-remote-client-6.5.0.Final
  • kie-remote-common-6.5.0.Final
  • kie-remote-jaxb-6.5.0.Final
  • kie-server-api-6.5.0.Final
  • kie-server-client-6.5.0.Final
  • optaplanner-core-6.5.0.Final

Environment: - JBoss Developer Studio 10.4 - Wildfly 10.1 - JDK 1.8

PS: I have been able to connect to the KieServer via REST, but it is with the discouraged code and probably because of that (I guess, nobody has ever answered me) I don't obtain the response I want.

回答1:

So I got the answer thanks to some guidelines I encountered on the RedHat webpage on Business Central: FIRING RULES USING KIE SERVER JAVA CLIENT API

Key in this case was adding the classes to the configuration options for the Kie client. Code below is valid for connecting with KIE server via REST when using Drools/Kie workbench 6.5.0. command is a valid BatchExecutionCommand:

KieServicesConfiguration config =  KieServicesFactory.
                newRestConfiguration(KieServer.urlKieServer,
                        KieServer.name,
                        KieServer.password);
config.setMarshallingFormat(MarshallingFormat.JAXB); 
config.setTimeout(3000L); //optional
Set<Class<?>> allClasses = new HashSet<Class<?>>();
allClasses.add(YOURCLASS.class);
config.addExtraClasses(allClasses);

KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
RuleServicesClient rulesClient = client.getServicesClient(RuleServicesClient.class);  

ServiceResponse<ExecutionResults> response = rulesClient.executeCommandsWithResults(containerName, command); 

Hope it helps someone.