我有一个接受Java对象的REST方法,通过规则引擎进行一些计算并返回另一个Java对象。
规则引擎正常工作并返回Java对象(我的Java类名为Tier)
在REST方法中,我成功地将规则引擎的输出检索为Tier对象,如下所示:
for (FactHandle fh : factHandlesTier) {
t = (Tier) ((DefaultFactHandle) fh).getObject();
}
我可以检查我的Tier类型的对象t确实在我打印时收到数据:System.out.println("Tier Category ===> " + t.getCategory() + " Tier Total ===> " + t.getTotal());
我得到了这个输出Tier Output ===> 2 Tier Total ===> 0.15000000000000002
但我的问题是,当涉及我的REST方法中的return语句时,返回的对象似乎被初始化并以某种方式丢失来自规则引擎的数据。
这是通过浏览器从我的REST方法输出的:{"category":0,"id":0,"quantity":0,"total":0.0,"value":0.0}
这是我的Tier Java类:`/ * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择“工具”|模板 *并在编辑器中打开模板。 * / 包za.co.ganizani.sponsorship.model.rules;
import java.io.Serializable;
public class Tier实现Serializable {
private int id;
private double value;
private int quantity;
private double total;
private int category;
private String type;
public Tier() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
} `
这是我的REST方法:
@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("/calculate")
@ApiOperation(value="/calculate", notes="Calulate Sponsorship values")
public static Tier CalculateSponsorship(Event e) {
ksession = kbase.newStatefulKnowledgeSession();
ksession.insert(e);
//insert categories in StatefulKnowledgeSession
for(int a = 0; a < categoryList.size(); a++){
ksession.insert(categoryList.get(a));
}
//insert tiers in StatefulKnowledgeSession
for(int b = 0; b < tierList.size(); b++){
ksession.insert(tierList.get(b));
}
ksession.fireAllRules();
//retrieve rules engine outputs
Collection<FactHandle> factHandlesTier;
factHandlesTier = ksession.getFactHandles(new ClassObjectFilter(Tier.class));
Tier t = new Tier();
for (FactHandle fh : factHandlesTier) {
t = (Tier) ((DefaultFactHandle) fh).getObject();
}
ksession.dispose();
System.out.println("Tier Category ===> " + t.getCategory() + " Tier Total ===> " + t.getTotal());
//t.setCategory(333);
return t;
}
感谢您的帮助!